gpt4 book ai didi

关闭并重新打开选项卡时 Flutter web shared_preferences 不可用

转载 作者:行者123 更新时间:2023-12-03 02:50:29 25 4
gpt4 key购买 nike

Flutter web 现在支持 shared_preferences .我已添加 shared_preferences到我的 flutter 网络应用程序。
我的问题是当我设置首选项时,比如当我启动应用程序时,它在应用程序的任何地方都可以正常工作,但是当我关闭浏览器并再次启动应用程序并尝试检索它时,它什么都不返回。如何在关闭并启动应用程序后使其工作。

_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 build web并提供来自 build/web 的文件目录。

当应用程序在 web-server 上启动时设备(即带有 flutter run --device web-server )首选项可能会丢失,因为服务器在任意端口启动。要修复它,请使用选项 --web-port 运行 flutter喜欢

flutter run -d web-server --web-port 3344

当应用在设备上启动时,无法在 session 之间保存首选项 chrome (命令 flutter run -d chrome)。
每次 chrome随机推出 user-data-dir参数和首选项仅在页面刷新后保留,但在重新启动后清除。

在源 code它解释为:

Using a tmp directory ensures that a new instance of chrome launches allowing for the remote debug port to be enabled.

关于关闭并重新打开选项卡时 Flutter web shared_preferences 不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59503499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com