Reputation:
I have to save data for an inventory. I organized them as a string list, how can I proceed? Reading and writing to a file does not seem to be suitable because writing will overwrite (so can’t easily update) and reading would cause the structure to be lost. Please help me!
Upvotes: 2
Views: 4794
Reputation: 10865
dependencies:
shared_preferences: ^0.5.2
$ flutter packages get
import 'package:shared_preferences/shared_preferences.dart';
Here is how you store and get the list of string back:
String listKey = "listKey";
void storeStringList(List<String> list) async{
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setStringList(listKey, list);
}
Future<List<String>> getStringList() async{
SharedPreferences prefs = await SharedPreferences.getInstance();
return await prefs.getStringList(listKey);
}
This is how you can get that list
getStringList((List<String> strList){
//strList is the string that you stored.
});
Upvotes: 2