0

I'm trying to save and load a List using the shared preferences package in flutter.

As shared preferences doesn't support saving Lists other than List<String> with setStringList() (and in my case, a List<String> won't work for what I need), I instead opted to convert my list to a string with .toString(), and saved it with setString().

This works just fine for saving it, but now I can't load the list for later.

I saw others recommend loading the String as JSON, but seemingly .toString() doesn't give me valid JSON, so that's not possible.

I also tried just casting the String to a list... unsurprisingly, that didn't work either.

I can't seem to find any way to convert a List to JSON so I can just save and load it that way, and I can't seemingly find any way to load a .toString()'d list as... well, a list.

I'm not sure what to do now, as I can't seem to find a way to load this List..

Any help would be greatly appreciated!

4 Answers 4

14

You can use jsonEncode and jsonDecode.

import 'dart:convert'; void main() { var list = [1, 2, 3, 4]; String json = jsonEncode(list); print(json); List list2 = jsonDecode(json); print(list2); } 

Here is the documentation https://flutter.dev/docs/development/data-and-backend/json

Sign up to request clarification or add additional context in comments.

3 Comments

This actually worked, I thought I'd tried jsonEncode before, but maybe i did it on the string? Either way, thanks! (I'm not marking this as answer just yet - wanna see if Eric Duffetts solution is better, but if anyone looking at this q rn has the same problem; this works.)
(okay, to keep more people from answering, im marking this one answer for now. but i still wanna look at erics solution)
[personal, public] it's not working when using string.
1

I don't think .toString() is going to be of any help. You need to move away from Shared_Preferences and find a way to persist data in a way that supports the data type you are working with. (What data type are you working with?)

Here is a guide from Flutter on creating persistence with SQFlite: https://flutter.dev/docs/cookbook/persistence/sqlite

Depending on what you need, Firebase may also be a solution for you.

3 Comments

Well, you're probably right that I shouldent be using shared_preferences for this, but the data right now is not supposed to be more than a few arrays of data, for keeping persistance of a scanned (thing barcodes in shops and what not) item list. A database just feels kinda overkill, since I won't be storing much other data... aside from, actual settings :^)
What data type is in the array?
a few (around 4) strings, a number, and array in the array containing a few bools (i.e product data, ean codes, image links, allergy info) this is kept server side, but is downloaded and kept around until the user clears the list containing recently scanned stuff
1

To convert your List<String> to a json string, you can do this :

import 'dart:convert'; List<String> myList = ...; String json = jsonEncode(myList); 

Then you'll be able to save it with setString()

To convert it back to a List, use jsonDecode(myString)

Comments

0

I can't understand why its not working for you, maybe you are missing something. You should save your json as a String using setString() method and when you want to get them, use getString() method. And boolean types also not supported, so for them you need to do this also. Take a look to this example, this is work perfect. I'm saving my Lists like this:

void _saveList() async { List<dynamic> list = ['1','2','3','4']; prefs = await SharedPreferences.getInstance(); prefs.setString('list', json.encode(list)); List<dynamic> stringList = json.decode(prefs.getString('list')); print(stringList.runtimeType); print(stringList); } 

The output:

List<dynamic> [1, 2, 3, 4] 

What you need it's just to convert your List to JSON and save it using setString() method, when you will need it , use getString() method and after decode the JSON. Also you need to set your List's generic type to <dynamic>, because after getting the list it becomes List<dynamic>.

Hope it will work.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.