Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save List<Object> to SharedPreferences in Flutter?

I have a list of favorite music, which I retrieve from music when the app is opened for the first time, the app gets a favorite music list from favorite. I want to save this list to shared

preferences.List<Music> favoriteMusic = new List<Music>();

where music class is:

class Music {
  final int id;
  final String name, size, rating, duration, img;
  bool favorite;

  Music({
    this.id,
    this.rating,
    this.size,
    this.duration,
    this.name,
    this.img,
    this.favorite,
  });

  factory Music.fromJson(Map<String, dynamic> jsonData){
    return Music(
      id: jsonData['id'],
      rating: jsonData['rating'],
      size: jsonData['size'],
      duration: jsonData['duration'],
      name: jsonData['name'],
      img: jsonData['img'],
      favorite: false,
    );
  }
}

How can I save favorite music list?

like image 926
sumseflut Avatar asked Apr 20 '20 06:04

sumseflut


People also ask

Can we store List in shared preferences Flutter?

The data stored in SharedPreferences can be edited and deleted. SharedPreferences stores the data in a key-value pair. To use SharedPreferences in Flutter, a plugin called shared_preferences enables us to store data. The plugin wraps NSUserDefaults on iOS and SharedPreferences on Android.

How do I save a List in shared preferences?

You can save String and custom array list using Gson library. =>First you need to create function to save array list to SharedPreferences. public void saveListInLocal(ArrayList<String> list, String key) { SharedPreferences prefs = getSharedPreferences("AppName", Context. MODE_PRIVATE); SharedPreferences.

How do you save List of int in SharedPreferences in Flutter?

Currently, there is not any option to directly save the list of ints in Sharedpreferences. You can save the list of Strings in sharedpreferences. The easiest way for your case is to convert your List<int> values to List<String> and store them.

Can SharedPreferences store objects?

We can store fields of any Object to shared preference by serializing the object to String.


Video Answer


5 Answers

You should do these steps

to save the object:

  1. convert your object to map with toMap() method
  2. encode your map to string with encode(...) method
  3. save the string to shared preferences

for restoring your object:

  1. decode shared preference string to a map with decode(...) method
  2. use fromJson() method to get your object

UPDATE FULL SAMPLE

import 'dart:convert';

void main() async {
  final SharedPreferences prefs = await SharedPreferences.getInstance();
  
  // Encode and store data in SharedPreferences
  final String encodedData = Music.encode([
    Music(id: 1, ...),
    Music(id: 2, ...),
    Music(id: 3, ...),
  ]);

  await prefs.setString('musics_key', encodedData);

  // Fetch and decode data
  final String musicsString = await prefs.getString('musics_key');

  final List<Music> musics = Music.decode(musicsString);
}

class Music {
  final int id;
  final String name, size, rating, duration, img;
  bool favorite;

  Music({
    this.id,
    this.rating,
    this.size,
    this.duration,
    this.name,
    this.img,
    this.favorite,
  });

  factory Music.fromJson(Map<String, dynamic> jsonData) {
    return Music(
      id: jsonData['id'],
      rating: jsonData['rating'],
      size: jsonData['size'],
      duration: jsonData['duration'],
      name: jsonData['name'],
      img: jsonData['img'],
      favorite: false,
    );
  }

  static Map<String, dynamic> toMap(Music music) => {
        'id': music.id,
        'rating': music.rating,
        'size': music.size,
        'duration': music.duration,
        'name': music.name,
        'img': music.img,
        'favorite': music.favorite,
      };

  static String encode(List<Music> musics) => json.encode(
        musics
            .map<Map<String, dynamic>>((music) => Music.toMap(music))
            .toList(),
      );

  static List<Music> decode(String musics) =>
      (json.decode(musics) as List<dynamic>)
          .map<Music>((item) => Music.fromJson(item))
          .toList();
}
like image 53
Hamed Avatar answered Oct 27 '22 06:10

Hamed


Convert it to a string, you can store it

import 'dart:convert';
...
var s = json.encode(myList);
// or var s = jsonEncode(myList);

json.decode() //convert a string to List when you load it
like image 33
Rudresh Narwal Avatar answered Oct 27 '22 05:10

Rudresh Narwal


Flutter's shared_preferences plugin has a method: setStringList(String key, List<String> value), so you can just write serializer for your objects.

like image 7
Michał Dobi Dobrzański Avatar answered Oct 27 '22 04:10

Michał Dobi Dobrzański


For noob folks like me who want to understand a bit more about the magic our dear friend Hamed did in his answer, or want to adapt his solution to more complex classes with lists/other classes, check out these two links: https://bezkoder.com/dart-flutter-parse-json-string-array-to-object-list/

https://bezkoder.com/dart-flutter-convert-object-to-json-string/

jsonEncode() and jsonDecode() are the same as json.encode() and json.decode()

like image 3
jayesh saha Avatar answered Oct 27 '22 06:10

jayesh saha


simply use stringlist in shared preferences

basic syntax:

    // read
    final myStringList = prefs.getStringList('my_string_list_key') ?? [];
    // write
    prefs.setStringList('my_string_list_key', ['a', 'b', 'c']);

Firstly convert the object to a map. Then convert the map to a JSON string using jsonEncode and at the end save the JSON string to shared preferences

Sample example:

        // import 'dart:convert';
    Person person = Person('Mary', 30);
    Map<String, dynamic> map = {
      'name': person.name,
      'age': person.age
    };
    String rawJson = jsonEncode(map);
    prefs.setString('my_string_key', rawJson);

 

retrieve data

final rawJson = prefs.getString('my_string_key') ?? '';
Map<String, dynamic> map = jsonDecode(rawJson);
final person = Person(map['name'], map['age']);
like image 2
Tarun Jain Avatar answered Oct 27 '22 04:10

Tarun Jain