我有两个共同的偏好
<string name="flutter.39281a04d2b8c8ba">{"name":"student","id":"UmWq","token":"LVF-M","isIncoming":true,"uuid":"39281a04d2b8c8ba"}</string>
<string name="flutter.4c434612edcff4bc">{"name":"student","id":"ozjU","token":"GXUTR","isIncoming":true,"uuid":"4c434612edcff4bc"}</string>我有一个id,我想从这个id中检索uuid,以便创建一个文件:
MySharedPreferences.getUuid(id).then((uuid) {
print("ended up with this uuid: $uuid");
FileManager.createFile(uuid, form.title).then((file) => {
file.writeAsString(str)
});
});这是getUuid方法:
static Future<String> getUuid(String id) async {
final prefs = await SharedPreferences.getInstance();
for (String key in prefs.getKeys()){
print("looking at $key");
if (!key.contains("#")){
return getData(key).then((value){
print("got this value: $value");
Map<String, dynamic> endpointJson = jsonDecode(value);
EndpointData endpoint = EndpointData.fromJson(endpointJson);
print("endpoint.id: ${endpoint.id}");
print("id: $id");
if (endpoint.id == id){
print("the good return");
return endpoint.uuid;
}
return null;
});
}else{
continue;
}
}
}如您所见,getUuid检索共享首选项中的所有键的列表,并对它们进行迭代。对于每个键,我读取相关的数据并检查id是否与我的匹配,因此返回uuid。
现在,我知道问题所在,但我找不到解决它的方法。问题如下。假设我的id是ozjU,我的getUuid函数会按顺序迭代键,所以如果第一次检查39281a04d2b8c8ba。39281a04d2b8c8ba的id不等于我的id,所以在getData内部返回null,这使得getUuid也返回null。我不知道如何让getData在endpoint.id == id或不返回任何东西的情况下返回值,并让外部的for继续迭代其余的键。
EDIT:根据要求,测试如下,但uuid仍然为空:
static Future<String> getUuid(String id) async {
final prefs = await SharedPreferences.getInstance();
for (String key in prefs.getKeys()){
print("looking at $key");
if (!key.contains("#")){
return getData(key).then((value){
print("got this value: $value");
Map<String, dynamic> endpointJson = jsonDecode(value);
EndpointData endpoint = EndpointData.fromJson(endpointJson);
print("endpoint.id: ${endpoint.id}");
print("id: $id");
if (endpoint.id == id){
print("the good return");
return endpoint.uuid;
}
});
}else{
continue;
}
}
return null;
}最终编辑:等待getData结果如下:
static Future<String> getUuid(String id) async {
final prefs = await SharedPreferences.getInstance();
for (String key in prefs.getKeys()){
print("looking at $key");
if (!key.contains("#")){
var value = await getData(key);
print("got this value: $value");
Map<String, dynamic> endpointJson = jsonDecode(value);
EndpointData endpoint = EndpointData.fromJson(endpointJson);
print("endpoint.id: ${endpoint.id}");
print("id: $id");
if (endpoint.id == id){
print("the good return");
return endpoint.uuid;
}
}else{
continue;
}
}
return null;
}发布于 2020-12-18 18:05:18
正确的代码:
static Future<String> getUuid(String id) async {
final prefs = await SharedPreferences.getInstance();
for (String key in prefs.getKeys()){
print("looking at $key");
if (!key.contains("#")){
var value = await getData(key);
print("got this value: $value");
Map<String, dynamic> endpointJson = jsonDecode(value);
EndpointData endpoint = EndpointData.fromJson(endpointJson);
print("endpoint.id: ${endpoint.id}");
print("id: $id");
if (endpoint.id == id){
print("the good return");
return endpoint.uuid;
}
}else{
continue;
}
}
return null;
}https://stackoverflow.com/questions/65325060
复制相似问题