[英]How to Save List in SharedPreferences in Flutter
Hello all at first I want to mention that I've tried a lot of solutions here but it didn't work for me.大家好,首先我想提一下,我在这里尝试了很多解决方案,但对我没有用。
I bring the list from the database through the following code:我通过以下代码从数据库中获取列表:
var listCat = [];
Future getdata() async {
apiURL = '***************.php';
var response = await http.post(Uri.parse(apiURL));
var responsebody = jsonDecode(response.body);
if (responsebody.length >0){
for (int i = 0; i < responsebody.length; i++) {
listCat.add(responsebody[i]['name']+ ':' + responsebody[i]['image'].toString());
}
return responsebody;
}else{
}
}
As is obvious in the code above I am trying to get the name and image and this is not a problem right now I want to store this listCat in SharedPreferences until I recall it from all pages of the app在上面的代码中很明显,我正在尝试获取名称和图像,现在这不是问题我想将此 listCat 存储在 SharedPreferences 中,直到我从应用程序的所有页面中调用它
I have the following class to save SharedPreferences:我有以下 class 来保存 SharedPreferences:
class APIPreferences {
static SharedPreferences ? _preferences;
static const _keyMuinCat = 'MuinCat';
static Future init() async => _preferences = await SharedPreferences.getInstance();
static Future setMuinCat(String MuinCat) async => await _preferences!.setString(_keyMuinCat, MuinCat);
static String? getMuinCat() => _preferences!.getString(_keyMuinCat);
}
Then I save what I need to save by the following line:然后我通过以下行保存我需要保存的内容:
APIPreferences.setMuinCat(listCat.toString());
Then I can bring pre-stored data from any location where I need it through the following code:然后我可以通过以下代码从我需要的任何位置获取预存储的数据:
CatList = APIPreferences.getMuinCat() ?? '';
I tried to do the following thing now to save the list in the first code above:我现在尝试执行以下操作以将列表保存在上面的第一个代码中:
var listCat = [];
Future getdata() async {
apiURL = '***************.php';
var response = await http.post(Uri.parse(apiURL));
var responsebody = jsonDecode(response.body);
if (responsebody.length >0){
for (int i = 0; i < responsebody.length; i++) {
listCat.add(responsebody[i]['name']+ ':' + responsebody[i]['image'].toString());
APIPreferences.setMuinCat(listCat.toString());
}
return responsebody;
}else{
}
}
But it didn't work.但它没有用。 I don't really know how to deal with it.我真的不知道如何处理它。
How can I save it and then bring it to use with ListView.如何保存它然后将其与 ListView 一起使用。
To save the list in shared preferences you need to pass as jsonEncode(yourList data) and when you will fecth the shared list you will again jsonDecode(your list)要将列表保存在共享首选项中,您需要作为 jsonEncode(yourList data) 传递,并且当您对共享列表进行验证时,您将再次 jsonDecode(your list)
await prefs.setString('YOUR KEY', json.encode(YOURMAP()));
instead of:代替:
_preferences!.setString(_keyMuinCat, "some string");
use:利用:
_preferences!.setStringList(_keyMuinCat, ["some", "strings", "in", "list"]);
So in your code, the setMuinCat
method needs to be:因此,在您的代码中, setMuinCat
方法必须是:
static Future setMuinCat(List<String> muinCat) async => await _preferences!.setStringList(_keyMuinCat, muinCat);
and then you call it like this:然后你这样称呼它:
APIPreferences.setMuinCat((listCat as List).map((v) => v.toString()).toList());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.