flutter - 为什么我不能用 shared_preferences 保存 int?
问题描述
我尝试构建一个简单的应用程序,它应该保存并输出一个value
whith shared_preferences
。我试图保存一个int
,但它不起作用。可能是,错误是因为我试图“转换”youtuber 用 aString
而不是int
. 有人能找到我的错误吗?以下是我尝试的更改代码。
int lastLoginInt = 1;
String nameKey = "_key_name";
@override
void initState() {
super.initState();
}
Future<bool> saveLastLoginInt() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
return await preferences.setInt(nameKey, lastLoginInt);
}
Future<int> loadLastLoginInt() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
return preferences.getInt(nameKey);
}
setLastLoginInt() {
loadLastLoginInt().then((value) {
setState(() {
lastLoginInt = value;
});
});
}
解决方案
你不是在调用函数。
可能你应该在你的initState()
功能上这样做..就像这样..
@override
void initState() {
super.initState();
saveLastLoginInt();
}
然后setLastLoginInt()
在需要的地方使用。
推荐阅读
- solr - SolrJ 和自定义 Solr 处理程序
- oracle - 在 oracle 中键入明智的求和和减法
- c# - 用 ':' 分隔符分割 1 个字符串
- java - Javafx:如果服务消息更新,标签变为空白(标签绑定到 msg)
- python - python中两个3D数组与NaN的皮尔逊相关性
- angular - Angular + Firebase:TypeError:this.productService.get(...).take 不是函数
- python-3.x - 是否可以在通用集中显示维恩图?
- java - Flink 1.4.2 SQL 映射?
- c - 对 size_t 感到困惑:在 32 位系统上它如何足够大?
- python - Django Rest Framework - CreateAPIView ForeignKey 查找字段