How can I save a list locally to a database with shared preferences in flutter . for example :- I have list List notification = []; How can I save this list locally with shared preferences?
CodePudding user response:
The plugin itself already has a method for storing List<String>
, it's called setStringList.
If you have some other type of data to store, you simply have to convert them in String
and have a function to convert them back in your data type.
The other question linked in the comments is just a collection of convenience methods to store different data types, but it's nothing you can't write on your own.
CodePudding user response:
const _notificationListKey='notificationList';
Future<void> notificationInLocalStorage( List notificationList ) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await Future.wait([
prefs.setString(_notificationListKey,json.encode( notificationList)),
]);
}