Dart 我可以把列表放在Flatter中的SharedReferences中吗?
我可以将Dart 我可以把列表放在Flatter中的SharedReferences中吗?,dart,flutter,sharedpreferences,Dart,Flutter,Sharedpreferences,我可以将ArrayList或List对象放置到SharedReferences中吗?因为我刚刚意识到,弗利特没有在他们的飞镖格森 所以..我想通过点击星号图标保存喜欢的书,当我点击星号图标时,这些书将保存在SharedReferences中 我可以看到我最喜欢的书的列表 保存: prefs.setStringList('key',yourList); 检索: var yourList=prefs.getStringList('key'); 按ArrayList如果您指的是颤振的列表数据结构,那
ArrayList
或List
对象放置到SharedReferences
中吗?因为我刚刚意识到,弗利特没有在他们的飞镖格森
所以..我想通过点击星号图标保存喜欢的书,当我点击星号图标时,这些书将保存在SharedReferences中
我可以看到我最喜欢的书的列表 保存:
prefs.setStringList('key',yourList);
检索:
var yourList=prefs.getStringList('key');
按ArrayList如果您指的是颤振的列表数据结构,那么您可以使用以下方法在SharedReference中仅保存列表:
Future <bool> setStringList(String key, List<String> value)
未来设置字符串(字符串键,列表值)
您可以通过以下方式获取列表:
List<String> getStringList(String key)
List getStringList(字符串键)
有关共享_首选项的更多信息,请参阅本文
一些免费建议:
SharedReference用于保存键值对中与用户设置相关的数据。它不应该保存任何复杂的数据,比如自定义对象列表
如果您需要存储比这更复杂的数据,请考虑使用SQLite .< /P> <代码>导入程序包:SyrdYPopys/SysDyPopys.DART;
import 'package:shared_preferences/shared_preferences.dart';
class MySharedPreferences {
MySharedPreferences._privateConstructor();
static final MySharedPreferences instance =
MySharedPreferences._privateConstructor();
setListData(String key, List<String> value) async {
SharedPreferences myPrefs = await SharedPreferences.getInstance();
myPrefs.setStringList(key, value);
}
Future<List<String>> getListData(String key) async {
SharedPreferences myPrefs = await SharedPreferences.getInstance();
return myPrefs.getStringList(key);
}
}
类MySharedPreferences{
MySharedPreferences._privateConstructor();
静态最终MySharedPreferences实例=
MySharedPreferences._privateConstructor();
setListData(字符串键、列表值)异步{
SharedReferences myPrefs=等待SharedReferences.getInstance();
myPrefs.setStringList(键、值);
}
未来getListData(字符串键)异步{
SharedReferences myPrefs=等待SharedReferences.getInstance();
返回myPrefs.getStringList(键);
}
}
更多参考:那么,我可以在一个ID中添加更多的int变量吗?我更新了代码,让我知道这是否适合您的需要。意思是我只能添加一本最喜欢的书,先生?在Flatter中,我们没有
数组列表
,我们只有列表
。只要它是一个字符串列表
,您就应该能够保存它。我发现这个错误,方法“setStringList”在null上被调用。Receiver:null尝试调用:setStringList(“list”,实例(长度:9)of'\u growtablelist')您好!欢迎来到SO。请看一看,为什么共享首选项不应该保存任何复杂的数据,如自定义对象列表??它是为了保存用户首选项而设计的,因此它基本上是为了保存键值对,如键:“主题”值:“暗”等。。等对于更复杂的结构,您应该使用像sqflite、moor这样的数据库,或者不使用像Hive这样的SQL数据库。您始终可以序列化数据并将其存储在SharedReferences中,但仅仅因为允许这样做并不意味着您应该这样做。如果你想保存复杂的数据,我会说选择hive。