如何在 shared_preferences 中创建布尔列表并在其中存储值
How to create a bool list in shared_preferences and store values there
required this.favorite,
我是这样从上一页得到布尔值的。因为我用的是pageview,所以我想把值像这样存到索引里,以后再用。
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])!;
});
}
而且我在上一页中这样使用上面的代码。这就是为什么我需要一个列表。没有它,我将不得不创建数百个页面。
void loadFavorite() async{
print(FavoriteButtons[0]);
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
favorite[0] = prefs.getBool(_favoriteButton[0])!;
是否可以从 shared_preferences 创建列表?我如何将 bool 存储为列表?
您可以尝试使用 SharedPreferences.setStringList,方法是仅将 true
最喜欢的按钮索引保存到列表中。像这样(详情见评论):
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;
}
请注意,代码尚未经过测试。
您只能在 SharedPreferences
中存储一个 List<String>
,但是当 reading/writing.
时,您可以轻松地将列表与布尔值相互转换
// List<bool> --> List<String>
boolValues.map((value) => value.toString()).toList();
// List<String> --> List<bool>
stringValues.map((value) => value == 'true').toList();
与其将其保存为列表,不如将其单独保存为索引
prefs.setBool('favoritrButton$index', isFavorite)
其中索引将是动态的。因此,当您检索保存的布尔值时,您可以使用
prefs.getBool('favoriteButton$index');
然后你可以使用类似
的保存方法
void save(int index, bool isFavorite) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool('favoriteButton$index', isFavorite);
}
并获得收藏夹
Future<Bool> isFavorite(int index) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getBool('favoritrButton$index')??true;
//?? true will return true if it's null
}
你可以通过这种方式保存bool list:
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();
});
}
如果将 bool 值存储在某些生成的键下,例如 index.toString();
,则可以构建可索引的 bool 列表
List boolist = List.empty(growable: true);
void loadList() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
prefs.getStringList('list')?.forEach((indexStr) {
//this will add the booleans stored in prefs under the indexStr key
boolist.add(prefs.getBool(indexStr));
});
});
您现在可以根据需要使用 boollist,完成后只需保存新的 prefs Stringlist 和布尔值
void saveList() async {
final prefs = await SharedPreferences.getInstance();
List<String> transfer = List<String>.empty(growable: true);
//this transfer list is needed so you can store your indexes as strings
int index = 0;
boollist.forEach((element) {
//here you just populate the transfer list for every value in in your boolist and save the appropiate boolean values
transfer.add(index.toString());
prefs.setBool(index.toString(), element);
index++
});
setState(() {
// save the new index string list
prefs.setStringList('list', transfer);
});
});
required this.favorite,
我是这样从上一页得到布尔值的。因为我用的是pageview,所以我想把值像这样存到索引里,以后再用。
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])!;
});
}
而且我在上一页中这样使用上面的代码。这就是为什么我需要一个列表。没有它,我将不得不创建数百个页面。
void loadFavorite() async{
print(FavoriteButtons[0]);
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
favorite[0] = prefs.getBool(_favoriteButton[0])!;
是否可以从 shared_preferences 创建列表?我如何将 bool 存储为列表?
您可以尝试使用 SharedPreferences.setStringList,方法是仅将 true
最喜欢的按钮索引保存到列表中。像这样(详情见评论):
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;
}
请注意,代码尚未经过测试。
您只能在 SharedPreferences
中存储一个 List<String>
,但是当 reading/writing.
// List<bool> --> List<String>
boolValues.map((value) => value.toString()).toList();
// List<String> --> List<bool>
stringValues.map((value) => value == 'true').toList();
与其将其保存为列表,不如将其单独保存为索引
prefs.setBool('favoritrButton$index', isFavorite)
其中索引将是动态的。因此,当您检索保存的布尔值时,您可以使用
prefs.getBool('favoriteButton$index');
然后你可以使用类似
的保存方法 void save(int index, bool isFavorite) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool('favoriteButton$index', isFavorite);
}
并获得收藏夹
Future<Bool> isFavorite(int index) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getBool('favoritrButton$index')??true;
//?? true will return true if it's null
}
你可以通过这种方式保存bool list:
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();
});
}
如果将 bool 值存储在某些生成的键下,例如 index.toString();
,则可以构建可索引的 bool 列表List boolist = List.empty(growable: true);
void loadList() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
prefs.getStringList('list')?.forEach((indexStr) {
//this will add the booleans stored in prefs under the indexStr key
boolist.add(prefs.getBool(indexStr));
});
});
您现在可以根据需要使用 boollist,完成后只需保存新的 prefs Stringlist 和布尔值
void saveList() async {
final prefs = await SharedPreferences.getInstance();
List<String> transfer = List<String>.empty(growable: true);
//this transfer list is needed so you can store your indexes as strings
int index = 0;
boollist.forEach((element) {
//here you just populate the transfer list for every value in in your boolist and save the appropiate boolean values
transfer.add(index.toString());
prefs.setBool(index.toString(), element);
index++
});
setState(() {
// save the new index string list
prefs.setStringList('list', transfer);
});
});