How to create a bool list in shared_preferences and store values there
153
Solution 1
You can hold bool list by this method:
List<bool> favorite = <bool>[];
Future<void> loadFavorite() async{
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
favorite = (prefs.getStringList("userFavorite") ?? <bool>[]).map((value) => value == 'true').toList();
});
}
Future<void> delete() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
favorite[index] = false;
});
await prefs.setStringList("userFavorite", favorite.map((value) => value.toString()).toList());
setState(() {
favorite = (prefs.getStringList("userFavorite") ?? <bool>[]).map((value) => value == 'true').toList();
});
}
Future<void> saved() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
favorite[index] = true;
});
await prefs.setStringList("userFavorite", favorite.map((value) => value.toString()).toList());
setState(() {
favorite = (prefs.getStringList("userFavorite") ?? <bool>[]).map((value) => value == 'true').toList();
});
}
Solution 2
You can try using SharedPreferences.setStringList by saving only the true
favorite button index to the list. Something like this (see comment for detail):
void save(int index, bool isFavorite) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
var favorites = prefs.getStringList('favorites')??[];
// index as string item
var strIndex = index.toString();
if(isFavorite) {
// Save index to list only if it it not exist yet.
if(!favorites.contains(strIndex)) {
favorites.add(strIndex);
}
} else {
// Remove only if strIndex exist in list.
if(favorites.contains(strIndex)) {
favorites.remove(strIndex);
}
}
// Save favorites back
prefs.setStringList('favorites', favorites);
}
Future<bool> isFavorite(int index) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
var favorites = prefs.getStringList('favorites')??[];
// index as string item
var strIndex = index.toString();
// If index is exist, then it is must be true.
if(favorites.contains(strIndex) {
return true;
}
return false;
}
// All item index in list is always true
Future<List<int>> getFavoriteIndexes() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
var favorites = prefs.getStringList('favorites')??[];
var indexes = <int>[];
for(var favIndex in favorites) {
// return -1 if invalid fav index found
int index = int.tryParse(favIndex) ?? -1;
if(val != -1) {
indexes.add(index);
}
}
return indexes;
}
Please be aware, that the code haven't been tested yet.
Author by
eno2
Updated on December 01, 2022Comments
-
eno2 over 1 year
required this.favorite,
I got the bool value from the previous page like this. Since I used the pageview, I want to store the value in the index like this and use it later.
loadFavorite() async{ SharedPreferences prefs = await SharedPreferences.getInstance(); setState(() { favorite= prefs.getBoolList(_favoriteButton[index])!; }); } void delete() async { SharedPreferences prefs = await SharedPreferences.getInstance(); await prefs.setBool(_favoriteButton[index], false); setState(() { favorite= prefs.getBool(_favoriteButton[index])!; }); } void saved() async { SharedPreferences prefs = await SharedPreferences.getInstance(); await prefs.setBool(_favoriteButton[index], true); setState(() { favorite= prefs.getBool(_favoriteButton[index])!; }); }
And I use the above code like this in the previous page. This is why I need a list. Without it I would have to create hundreds of pages.
void loadFavorite() async{ print(FavoriteButtons[0]); SharedPreferences prefs = await SharedPreferences.getInstance(); setState(() { favorite[0] = prefs.getBool(_favoriteButton[0])!;
Is it possible to create a list from shared_preferences? And how can I store bool as a list?
-
eno2 over 2 yearsIs the above code saved in this way 'favorite[index]'? The code is too long and I haven't figured it out yet.
-
Røhäñ Dås over 2 yearsPreferred to save encoded map as string using setString
-
eno2 over 2 yearsHow can I save a List<bool> favorite in (prefs.getStringList("userFavorite")? I get an error because List<bool> favorite is empty with [] on the first run of the app.
-
Røhäñ Dås over 2 yearsyou can do null handling like this prefs.getStringList("userFavorite") ?? <bool>[]