I tried to safe two ints
with SharedPreferences
in one file. I don´t know if that is possible, because I can only save one of them. I´m not sure if I made a mistake in the code or if it´s not possible to save to different values in one StatefulWidget
. Does anybody see a mistake in my code or can tell me if it is theoretically possible to safe two values in one file? Below is the code of my two safe requests.
安全请求一项(一项有效):
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;
});
});
}
这是我的第二个请求(不起作用):
String nameKey1 = "_key_name2";
int countInt = 0;
Future<bool> saveCount() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
return await preferences.setInt(nameKey1, countInt);
}
Future<int> loadCount() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
return preferences.getInt(nameKey1);
}
setCount() {
loadLastLoginInt().then((value) {
setState(() {
countInt = value;
});
});
}
希望这是一个易于解决的问题。提前致谢!
Your
loadLastLoginInt
uses keynameKey
while yoursaveCount
usenameKey1
.nameKey = "_key_name"
nameKey1 = "_key_name2
尝试对两个变量使用相同的值。
Or Allow your setCount call
loadCount
instead ofloadLastLoginInt
是的,它的可能和允许的。我在设置页面等时已从SharedPrefs加载了多个值。
You're just calling
loadLastLoginInt()
instead ofloadCount()