如何在 Flutter 的 SharedPrefences 中正确存储整数而不获取空值?
How do I store an integer correctly in the SharedPrefences in Flutter without getting a null?
我想保存一个可以在新 class 中重用的 Int。为此,我使用了 SharedPreferences
。问题是当我想在我的新页面上打开 Int 时,我只得到一个空值。
但是我注意到,当我进行热重启然后切换到页面时,没有出现空值,而是我之前保存的内容。我的错误在哪里?
这里我保存值:
Future<Album> fetchAlbum() async {
int msgId;
//I fetch json from a page and store the value at msgId. I just don't have it in my code sample in here
SharedPreferences prefs = await SharedPreferences.getInstance();
msgId = (prefs.getInt('msgId'));
msgId = (prefs.getInt('msgId') ?? jsonData[0]["msgId"]);
prefs.setInt('msgId', msgId);
}
这里我取回保存的值(在一个新的页面上):
String url ='MyUrl';
int msgId;
// int intMsgId;
_loadCounter() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
msgId = (prefs.getInt('msgId'));
prefs.setInt('msgId', msgId);
print(msgId);
});
}
Future<String> makeRequest(String text) async {
_loadCounter();
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
msgId = (prefs.getInt('msgId'));
prefs.setInt('msgId', msgId);
print(msgId);
});
print("------MSG_ID------");
print(msgId);
print("------MSG_ID------");
//print(msgId.length);
if (msgId != null) {
var response = await http.post(Uri.encodeFull(url),
headers: {
"x-requested-with": "xmlhttprequest",
"Accept": "application/json",
"content-type": "application/json",
},
body: jsonEncode({
"messages": {
"msgId": msgId,
"refId": msgId
}
}));
print(response.body);
}
}
对于你的情况,我会这样做:
// other UI code
child: FutureBuilder(
future: prefs.getInt('msgId'), // your future
builder: (context, snapshot) {
if (snapshot.hasData) {
return Center(child: Container(child: Text('data: ${snapshot.data}')));
} else {
// We can show the loading view until the data comes back.
return CircularProgressIndicator();
}
},
),
问题可能是因为您没有await
SharedPreferences.setInt
方法。
您的代码:
prefs.setInt('msgId', msgId);
更改为:
await prefs.setInt('msgId', msgId);
因为 SharedPreferences.setInt
是异步的。
我想保存一个可以在新 class 中重用的 Int。为此,我使用了 SharedPreferences
。问题是当我想在我的新页面上打开 Int 时,我只得到一个空值。
但是我注意到,当我进行热重启然后切换到页面时,没有出现空值,而是我之前保存的内容。我的错误在哪里?
这里我保存值:
Future<Album> fetchAlbum() async {
int msgId;
//I fetch json from a page and store the value at msgId. I just don't have it in my code sample in here
SharedPreferences prefs = await SharedPreferences.getInstance();
msgId = (prefs.getInt('msgId'));
msgId = (prefs.getInt('msgId') ?? jsonData[0]["msgId"]);
prefs.setInt('msgId', msgId);
}
这里我取回保存的值(在一个新的页面上):
String url ='MyUrl';
int msgId;
// int intMsgId;
_loadCounter() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
msgId = (prefs.getInt('msgId'));
prefs.setInt('msgId', msgId);
print(msgId);
});
}
Future<String> makeRequest(String text) async {
_loadCounter();
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
msgId = (prefs.getInt('msgId'));
prefs.setInt('msgId', msgId);
print(msgId);
});
print("------MSG_ID------");
print(msgId);
print("------MSG_ID------");
//print(msgId.length);
if (msgId != null) {
var response = await http.post(Uri.encodeFull(url),
headers: {
"x-requested-with": "xmlhttprequest",
"Accept": "application/json",
"content-type": "application/json",
},
body: jsonEncode({
"messages": {
"msgId": msgId,
"refId": msgId
}
}));
print(response.body);
}
}
对于你的情况,我会这样做:
// other UI code
child: FutureBuilder(
future: prefs.getInt('msgId'), // your future
builder: (context, snapshot) {
if (snapshot.hasData) {
return Center(child: Container(child: Text('data: ${snapshot.data}')));
} else {
// We can show the loading view until the data comes back.
return CircularProgressIndicator();
}
},
),
问题可能是因为您没有await
SharedPreferences.setInt
方法。
您的代码:
prefs.setInt('msgId', msgId);
更改为:
await prefs.setInt('msgId', msgId);
因为 SharedPreferences.setInt
是异步的。