使用共享首选项保存列表时遇到问题(仍需要帮助)
Posted
技术标签:
【中文标题】使用共享首选项保存列表时遇到问题(仍需要帮助)【英文标题】:Have a problem saving a List using shared Preferences (Still need help) 【发布时间】:2020-01-04 15:23:50 【问题描述】:所以我坐了大约两天,试图使用共享偏好保存一个小列表。在下面的代码中,您可以看到许多循环,我知道这些循环效率不高,但它们在我的情况下工作得很好,因此无需研究它们。在我完成对我的算法使用循环后,我想保存输出以供下次使用。我要保存的列表是“list5”。我试着像这样保存它,但它对我没有用。 我也遇到了“.isNotEmpty”返回空错误的问题。
提前谢谢你。
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class Result extends StatelessWidget
List<String> people1;
List<bool> peoplePres;
Result(this.people1, this.peoplePres);
List<String> lastPres;
void initState()
lastZakif();
void lastZakif() async
SharedPreferences prefs = await SharedPreferences.getInstance();
lastPres = (prefs.getStringList('lastPres') ?? 0);
void _loadingNew(List<String> list5) async
SharedPreferences prefs = await SharedPreferences.getInstance();
lastPres = list5;
await prefs.setStringList('lastPres', lastPres);
@override
Widget build(BuildContext context)
int count = 0; //count for how many people are here
int count2 = 1; //count for the new order
int count4 = 0; //count for last schedule
List<int> list1 = new List(people1.length); //List with the algorithm
List<int> list2 = new List(people1.length); //List with the new order
List<String> list4 = new List(people1.length); // for loading last schedule
if (lastPres.isNotEmpty==true)
for (int i = 0; i < people1.length; i++)
//loading the last schedule and adding people if neccesary part 1
for (int j = 0; j < people1.length; j++)
if (people1[j] == lastPres[i])
list4[count4] = lastPres[i];
count4++;
bool check1 = false; //true if person exists
for (int i = 0; i < people1.length; i++)
//loading the last schedule and adding people if neccesary part 2
for (int j = 0; j < people1.length; j++)
if (people1[i] == list4[j])
check1 = true;
if (check1 == false)
list4[count4] = people1[i];
count4++;
check1 = false;
people1 = list4;
for (int i = 0; i < people1.length; i++)
//counting People that are here
if (peoplePres[i] == true)
count++;
if (count == people1.length)
count2 = 0;
for (int i = 0; i < people1.length; i++)
// Declaring a list which will be the index for the next Zkifut
list1[i] = i;
for (int i = 0; i < people1.length; i++)
//Applying the algorithm
int num1 = count ~/ 3;
if (count % 3 == 2)
num1 += 1;
list1[i] = list1[i] + num1 - count;
for (int i = 0; i < people1.length; i++)
// making the new schedule for absent people but starting with 0
if (peoplePres[i] == false)
list2[i] = 0;
break;
for (int i = 0; i < people1.length; i++)
// makeing the new schedule pos num
if ((list1[i] >= 0) && (peoplePres[i] == true))
list2[i] = count2;
count2++;
for (int i = 0; i < people1.length; i++)
// makeing the new schedule neg num
if ((list1[i] < 0) && (peoplePres[i] == true))
list2[i] = count2;
count2++;
for (int i = 0; i < people1.length; i++)
// makeing the new schedule for absent people
if ((peoplePres[i] == false) && (list2[i]) != 0)
list2[i] = count2;
count2++;
int count3 = 0;
List<String> list3 = new List(count);
for (int i = 0; i < people1.length; i++)
if (peoplePres[list2[i]] == true)
list3[count3] = people1[list2[i]];
count3++;
List<String> list5 = new List(people1.length); // for saving algorithm
for (int i = 0; i < people1.length; i++)
list5[i] = people1[list2[i]];
_loadingNew(list5);
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text(
'Result',
style: TextStyle(fontSize: 30),
),
),
body: ListView.builder(
itemCount: count,
itemBuilder: (context, value)
return Card(
color: Colors.amberAccent[200],
elevation: 3,
child: Container(
child: ListTile(
leading: Text('$value + 1'),
title: Text(list3[value]
//people1[value],
),
),
),
);
),
);
【问题讨论】:
【参考方案1】:这就是您如何将您的list
保存在SharedPreferences
中,如下所示:
void _loadingNew(List<String> list5) async
SharedPreferences prefs = await SharedPreferences.getInstance();
SharedPreferences.Editor editor = prefs.edit();
Gson gson = new Gson();
String json = gson.toJson(list5);
editor.putString(key, json);
editor.apply(); // This line is IMPORTANT !!!
参考:https://gist.github.com/WrathChaos/5f39e3ce3874a049d25e2ca8958d18b6
还有关于isNotEmpty()
根据@gprasant Check whether a string is not null and not empty的回答@
isNotEmpty()
只检查输入参数是否为
-
不为空
不是空字符串 ("")
检查列表是否为空的最佳方法是:
如果你只想在列表为空的情况下这样做,写起来更安全:
if (list != null && list.isEmpty()) do something
如果你想在列表为空或为空的情况下做某事,你可以这样写:
if (list == null || list.isEmpty()) do something
如果你想在列表不为空的情况下做某事,你可以写:
if (list != null && !list.isEmpty()) do something
参考:Two ways to check if a list is empty - differences?
希望对您有所帮助。
【讨论】:
为什么要使用ArrayList,有什么区别? 你看我只需要保存一个字符串列表,这对于使用共享首选项没有问题我认为没有理由使用 arrayList 你是对的@Dan7nm,你也可以使用列表,有关更多参考,请参阅此页面 - androidopentutorials.com/… 他们已经展示了如何使用列表将数据保存在 Sharedpreference 中。如果您仍有任何疑问,请告诉我。 仅供参考,请参阅此页面 - ***.com/questions/14903145/… 以了解 list 和 arraylist 之间的区别以及实现两者可以实现的目标。 @Dan7nm 我的回答对你有帮助吗?以上是关于使用共享首选项保存列表时遇到问题(仍需要帮助)的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 dart/flutter 中的共享首选项保存和获取列表列表
访问共享首选项时 Android 中的 ConcurrentModificationException