0

i want to persist a list that has custom objects and i even wrote method for serializing and de-serializing it but i don't know how to use it with SharedPreferences and FutureBuilder

here is the custom object:

 class Fact {
  String factText;
  Color factColor;

  Fact(this.factText, this.factColor);

  Map<String, dynamic> toJson() =>
      {'factText': factText, 'factColor': factColor};

  Fact.fromJson(Map<String, dynamic> json)
      : factText = json['factText'],
        factColor = json['factColor'];
}
5
  • 1
    Same as https://stackoverflow.com/questions/60192099/create-json-arrays-and-list-with-sharedpreferences/60237867#60237867 Commented Feb 23, 2020 at 14:32
  • and why dont you want to use a simple flat file instead? Commented Feb 23, 2020 at 15:13
  • @pskink isn't shared preferences sufficient enough? Commented Feb 23, 2020 at 16:17
  • 1
    shared preferences are implemented as xml files (at least on android) - so i dont think its the best for storing large amount of data Commented Feb 23, 2020 at 16:19
  • @pskink but i still need to do serialization right? Commented Feb 24, 2020 at 2:21

1 Answer 1

1

If you want to store a list inside shared_preference, then you can use the method setStringList():

  /// Saves a list of strings [value] to persistent storage in the background.
  ///
  /// If [value] is null, this is equivalent to calling [remove()] on the [key].
  Future<bool> setStringList(String key, List<String> value) =>
      _setValue('StringList', key, value);
Sign up to request clarification or add additional context in comments.

2 Comments

So I don't need to serialize my object?
you still need to use fromjson

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.