'How to save a list locally to a database with shared preferences in flutter?
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?
Solution 1:[1]
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.
Solution 2:[2]
const _notificationListKey='notificationList';
Future<void> notificationInLocalStorage( List notificationList ) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await Future.wait([
prefs.setString(_notificationListKey,json.encode( notificationList)),
]);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | il_boga |
| Solution 2 | Sachin Patil |
