【发布时间】:2021-07-15 05:14:34
【问题描述】:
我在我的页面中创建了一个 initState,并在每次打开此页面时调用 callData 以获取 favId(类型:列表)。但是,当应用程序启动时,我的编译器会显示此错误消息:
_TypeError (type 'List<String>' is not a subtype of type 'String')
这是我的 getData 函数:
getData(favId) async {
SharedPreferences pref = await SharedPreferences.getInstance();
return pref.getStringList(favId);
}
这也是我的 saveData 的功能:
void saveData() async {
SharedPreferences pref = await SharedPreferences.getInstance();
pref.setStringList("id", favId);
}
如何解决此问题,并且每次在应用程序中打开此页面时都可以调用 getData? 谢谢你:)
【问题讨论】:
-
请不要把 Dart 当作 JavaScript,仅仅因为你可以。 Dart 是一种正确的编程语言,使用类型而不是自动默认的“动态”。类型系统将帮助您处理正确的错误消息,因此您可以自己找出问题所在。
-
@nvoigt 好的,谢谢你的建议 :)