flutter - Flutter SharedPreferences 值在应用程序开始时提供给 Provider
问题描述
我正在尝试在应用程序启动时设置从 sharedpreferences 到 provider 的值。
这是我到目前为止所拥有的,对小部件的 sharedpreferences 正在工作: https ://gist.github.com/andraskende/a19c806aeef0ce88e9a9cafa49660ab4#file-main-dart-L211-L223
解决方案
Finally i figured out with trial and error... It can be done in the constructor as:
class BarcodeProvider with ChangeNotifier {
BarcodeProvider() {
setup();
}
void setup() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String url = (await prefs.getString('url') ?? '');
_url = url;
notifyListeners();
}
......
}
推荐阅读
- continuous-integration - 如何避免“此管道没有阶段/作业”。在 GitLab CI
- python - 值类型错误 Python3:只能连接元组
- flutter - 当我使用 facebook flutter 登录时出现“FacebookLoginStatus.error”
- database - 出现错误:错误状态:无法获取 DocumentSnapshotPlatform 上不存在的字段
- jprofiler - JProfiler:为什么代理类不显示在调用树中?
- javascript - 如何在不滚动的情况下按下按钮上下移动
- javascript - 我想用相同的按键操作按顺序交替执行不同的操作过程
- python - 列表包含具有相同值的数组
- laravel - 将 aws-s3-v3 与 laravel 和 digitalocean 空间一起使用会导致错误 501 未实现
- c# - UWP 项目导航