Flutter 和共享首选项:如何使用 setStringList 保存和读回列表 <Object>?另外,如何从列表中删除特定的 object?
Flutter & Shared Preferences: How do i save and read back a List<Object> using setStringList? Also, how do i remove a specific object from the List?
class Restaurant {
Restaurant({
required this.name,
required this.description,
required this.address,
required this.imageUrl,
});
Restaurant.fromJson(Map<String, Object?> json)
: this(
name: json['name']! as String,
description: json['description']! as String,
address: json['address']! as String,
imageUrl: json['imageUrl'] as String,
);
final String name;
final String description;
final String address;
final String imageUrl;
Map<String, Object?> toJson() {
return {
'name': name,
'description': description,
'address': address,
'imageUrl': imageUrl,
};
}
}
我从 Firebase 获取餐厅列表,当用户单击“收藏夹”图标时,我想在本地保存该餐厅并检索最喜欢的餐厅列表并将其显示在另一个页面上。我知道我可以直接在 Firebase 上完成,但现在,我希望它保存在本地。
期待收到大家的来信。谢谢。
像这样
List<Restaurant> restaturants = [Restuarant(...),Restuarant(...)];
List<String> encodedRestaturants = restaturants.map((res)=>json.encode(res.toJson())).toList();
//to write
prefs.setStringList("restaturants",encodedRestaturants);
//to read
List<String> decodedRestaturantsString = prefs.getStringList("restaturants");
List<Restaurant> decodedRestaturants = decodedRestaturantsString.map((res)=>Restaturant.fromJson(json.decode(res))).toList();
您可以使用 setString
.
而不是使用 SharedPreferences setStringList
此处摘录使用 setStringList
:
// sample restaurants.
List<Restaurant> favoriteRestos = [resto1, resto2, resto3];
List<String> favorites = [];
// Generate json for each restaurant
for(var resto in favoriteRestos) {
var json = jsonEncode(resto);
favorites.add(json);
}
// Obtain shared preferences.
final prefs = await SharedPreferences.getInstance();
// saving
await prefs.setStringList('favorites', favorites);
// Reading part
List<String> jsonRestos = prefs.getStringList('favorites')??[];
List<Restaurant> resFavorites = [];
for(var jsonResto in jsonRestos) {
var map = jsonDecode(jsonResto);
var resto = Restaurant.fromJson(map);
resFavorites.add(resto);
}
对于setString
:
List<Restaurant> favoriteRestos = [resto1, resto2, resto3];
var json = jsonEncode(favoriteRestos);
// Obtain shared preferences.
final prefs = await SharedPreferences.getInstance();
// saving
await prefs.setString('favorites', json);
// reading
var resJson = prefs.getString('favorites')??'';
var parsedJson = jsonDecode(resJson);
List<Restaurant> items = List<Restaurant>.from(parsedJson.map((i) => Restaurant.fromJson(i)));
注意:
您需要更新 Restaurant
class。参见 https://docs.flutter.dev/development/data-and-backend/json#serializing-json-inside-model-classes
class Restaurant {
Restaurant({
required this.name,
required this.description,
required this.address,
required this.imageUrl,
});
Restaurant.fromJson(Map<String, Object?> json)
: this(
name: json['name']! as String,
description: json['description']! as String,
address: json['address']! as String,
imageUrl: json['imageUrl'] as String,
);
final String name;
final String description;
final String address;
final String imageUrl;
Map<String, Object?> toJson() {
return {
'name': name,
'description': description,
'address': address,
'imageUrl': imageUrl,
};
}
}
我从 Firebase 获取餐厅列表,当用户单击“收藏夹”图标时,我想在本地保存该餐厅并检索最喜欢的餐厅列表并将其显示在另一个页面上。我知道我可以直接在 Firebase 上完成,但现在,我希望它保存在本地。
期待收到大家的来信。谢谢。
像这样
List<Restaurant> restaturants = [Restuarant(...),Restuarant(...)];
List<String> encodedRestaturants = restaturants.map((res)=>json.encode(res.toJson())).toList();
//to write
prefs.setStringList("restaturants",encodedRestaturants);
//to read
List<String> decodedRestaturantsString = prefs.getStringList("restaturants");
List<Restaurant> decodedRestaturants = decodedRestaturantsString.map((res)=>Restaturant.fromJson(json.decode(res))).toList();
您可以使用 setString
.
setStringList
此处摘录使用 setStringList
:
// sample restaurants.
List<Restaurant> favoriteRestos = [resto1, resto2, resto3];
List<String> favorites = [];
// Generate json for each restaurant
for(var resto in favoriteRestos) {
var json = jsonEncode(resto);
favorites.add(json);
}
// Obtain shared preferences.
final prefs = await SharedPreferences.getInstance();
// saving
await prefs.setStringList('favorites', favorites);
// Reading part
List<String> jsonRestos = prefs.getStringList('favorites')??[];
List<Restaurant> resFavorites = [];
for(var jsonResto in jsonRestos) {
var map = jsonDecode(jsonResto);
var resto = Restaurant.fromJson(map);
resFavorites.add(resto);
}
对于setString
:
List<Restaurant> favoriteRestos = [resto1, resto2, resto3];
var json = jsonEncode(favoriteRestos);
// Obtain shared preferences.
final prefs = await SharedPreferences.getInstance();
// saving
await prefs.setString('favorites', json);
// reading
var resJson = prefs.getString('favorites')??'';
var parsedJson = jsonDecode(resJson);
List<Restaurant> items = List<Restaurant>.from(parsedJson.map((i) => Restaurant.fromJson(i)));
注意:
您需要更新 Restaurant
class。参见 https://docs.flutter.dev/development/data-and-backend/json#serializing-json-inside-model-classes