【发布时间】:2020-05-03 23:37:19
【问题描述】:
我想要做的是获取 SharedPreferences stringList 但发生异常。我正在尝试解决它但被卡住了(我是菜鸟)。这是代码:
List<String> name;
getName() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
if (prefs.containsKey("name")) {
name = prefs.getStringList("name");
}
return [];
}
@override
void initState() {
this.getName();
super.initState();
}
@override
Widget build(BuildContext context) {
return name.isEmpty
? Container(
color: Colors.white,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset("assets/no-data.png"),
Text("No Seeds Saved", style: TextStyle(fontSize: 20))
],
),
),
)
: ListView.builder(
itemCount: name.length,
itemBuilder: (context, index) {
final item = name[index];
return Container(
padding: EdgeInsets.all(8),
child: Card(
color: Colors.teal[600],
child: ListTile(
title: Text(item,
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold)),
trailing: IconButton(
icon: Icon(Icons.remove_circle,
color: Colors.redAccent[400]),
onPressed: () {}),
),
),
);
},
);
}
发生了异常。 _TypeError(类型'String'不是类型'List'的子类型)
在构建 _BodyBuilder 时引发了以下 _TypeError: “Future”类型不是“List”类型的子类型
你能帮帮我吗?
【问题讨论】:
-
请同时显示您使用返回值的代码
-
好吧,编辑好了,我只在其他地方的小部件构建上使用它,这是问题吗?我使用它的方式或其他方式。我想不通
-
@YouOne 你能显示你保存列表的代码吗?
-
编辑过的朋友看看。对不起,我的代码很乱
-
是不是因为在我更改为 List
之前,我使用 String 来存储具有相同键(“名称”)的值。我是共享首选项的新手,是这样吗?我不确定