0

How can I save values to a list with SharedPreferences?

It's a simple list with a date and one value, for example, a list of my weight and sorting the list by date.

Vishnu Haridas
  • 7,355
  • 3
  • 28
  • 43
Marcel
  • 75
  • 1
  • 9
  • 1
    you can check this answer: https://stackoverflow.com/questions/61316208/how-to-save-listobject-to-sharedpreferences-in-flutter – wafaa sisalem Apr 24 '22 at 23:25

1 Answers1

0

After searching a lot of articles here you are

For Saving data, must be converted to JSON

 SharedPreferences prefs = await SharedPreferences.getInstance();

   Map<String, dynamic> user = 
    {'weight':[60,70],'height':[70,90]};

bool result = await prefs.setString('user', jsonEncode(user));

For Gettin data, must Deconverted from JSON

String userPref = prefs.getString('user');
     
Map<String,dynamic> userMap = jsonDecode(userPref) as Map<String, dynamic>;

or

final prefs = await SharedPreferences.getInstance();
await prefs.setStringList('items', <String>['Earth', 'Moon', 'Sun']);
DiyorbekDev
  • 706
  • 1
  • 5
  • 19