【发布时间】:2020-04-17 14:04:13
【问题描述】:
Flutter web 现在支持 shared_preferences。我已将 shared_preferences 添加到我的 Flutter Web 应用程序中。
我的问题是当我设置首选项时,比如说当我启动应用程序时,它在应用程序的任何地方都可以正常工作,但是当我关闭浏览器并再次启动应用程序并尝试检索它时,它什么也不返回。如何在关闭和启动应用程序后让它工作。
_showPref()async{
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setInt("myKey", 98);
}
@override
void initState() {
// TODO: implement initState
super.initState();
_showPref();
}
// then retrieve with the code
getPrefs(){
SharedPreferences prefs = await SharedPreferences.getInstance();
int count = (prefs.getInt("myKey")??0);
print(" 2. new prefs: $count");
}
【问题讨论】:
-
shared_preferences 在 Flutter Web 上对你有用吗?
-
我也发生了同样的事情。 shared_preferences 在会话中有效,但是当您关闭会话并重新打开它时,shared_preference 值就消失了。 Share_preferences 在本地或 chrome 中的服务器上不起作用
标签: flutter dart flutter-web