Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 如何使用SharedReferences保存颤振中列表中的多个布尔_List_Flutter_Sharedpreferences - Fatal编程技术网

List 如何使用SharedReferences保存颤振中列表中的多个布尔

List 如何使用SharedReferences保存颤振中列表中的多个布尔,list,flutter,sharedpreferences,List,Flutter,Sharedpreferences,我已经创建了一个bool列表: List<bool> switchbool = [true, true, true, true]; ) 但它不会加载存储在我保存的SharedReferences中的true或false Future<void> _getBoolFromSharedPref() async { final prefs = await SharedPreferences.getInstance(); switchbool.length = app.leng

我已经创建了一个bool列表:

List<bool> switchbool = [true, true, true, true];
)

但它不会加载存储在我保存的SharedReferences中的true或false

Future<void> _getBoolFromSharedPref() async {
final prefs = await SharedPreferences.getInstance();
switchbool.length = app.length;
for (var i = 0; i < app.length; i += 1) {
  final myBool = prefs.getBool(app[i]) ?? false;
}
Future\u getBoolFromSharedPref()异步{
final prefs=wait SharedPreferences.getInstance();
switchbool.length=app.length;
对于(变量i=0;i
} 这是_saveBoolFromSharedPref

Future<bool> _saveBoolFromSharedPref(bool value, String appName) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setBool(appName, value);
//print(switchbool);
Future\u saveBoolFromSharedPref(bool值,字符串appName)异步{
final prefs=wait SharedPreferences.getInstance();
等待prefs.setBool(appName,value);
//打印(switchbool);
}

任何建议都会很好,谢谢!
如果我可以从交换机上的值字段中检索数据,但我没有看到任何答案…

也许可以将您的值存储在地图中,然后对其进行解码/编码。例如:

var jsn = prefs.getString('Map');
Map = json.decode(jsn);


 var jsn = json.encode(Map);
 prefs.setString('Map', jsn);

如果有帮助,请告诉我。

为您的
\u getBoolFromSharedPref
方法尝试此方法

Future<void> _getBoolFromSharedPref() async {
final prefs = await SharedPreferences.getInstance();
switchbool.length = app.length;
for (var i = 0; i < app.length; i += 1) {
  switchbool[i] = prefs.getBool(app[i]) ?? false;
}
Future\u getBoolFromSharedPref()异步{
final prefs=wait SharedPreferences.getInstance();
switchbool.length=app.length;
对于(变量i=0;i
你也可以添加你的_saveBoolFromSharedPref方法吗?@zeus应该可以吧?检查我的答案@Malopieds。首先谢谢你的回复,但我不明白我应该如何使用地图,我还没有看到在地图中存储bool的可能性,但我必须将其转换为字符串?对不起,我不熟悉flifter,但非常感谢!de-和encoding提供了将其存储在SharedReferences中的可能性…也许你也可以用列表来做同样的事情,我不确定我是否能很好地适应网站,并且一切都正常。谢谢!我已经尝试过了,但是谢谢,看起来列表切换Bool是第一位的,而_GetBoolFromSharedRefer甚至没有被听到:/@Malopieds对不起,我不明白你的意思n通过“是第一个,而_getBoolFromSharedPref甚至没有被听到”是的,我不太清楚我的意思是,该列表对未来的空白具有优先权,因此它总是存储为“false”。但是rottler35的答案非常有魅力,但感谢您的帮助!
Future<void> _getBoolFromSharedPref() async {
final prefs = await SharedPreferences.getInstance();
switchbool.length = app.length;
for (var i = 0; i < app.length; i += 1) {
  switchbool[i] = prefs.getBool(app[i]) ?? false;
}