首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >然后在未来中返回未来

然后在未来中返回未来
EN

Stack Overflow用户
提问于 2020-12-16 22:19:58
回答 1查看 49关注 0票数 0

我有两个共同的偏好

代码语言:javascript
复制
<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,以便创建一个文件:

代码语言:javascript
复制
MySharedPreferences.getUuid(id).then((uuid) {
   print("ended up with this uuid: $uuid");
   FileManager.createFile(uuid, form.title).then((file) => {
       file.writeAsString(str)
   });
});

这是getUuid方法:

代码语言:javascript
复制
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

现在,我知道问题所在,但我找不到解决它的方法。问题如下。假设我的idozjU,我的getUuid函数会按顺序迭代键,所以如果第一次检查39281a04d2b8c8ba39281a04d2b8c8ba的id不等于我的id,所以在getData内部返回null,这使得getUuid也返回null。我不知道如何让getDataendpoint.id == id或不返回任何东西的情况下返回值,并让外部的for继续迭代其余的键。

EDIT:根据要求,测试如下,但uuid仍然为空:

代码语言:javascript
复制
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结果如下:

代码语言:javascript
复制
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;
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-18 18:05:18

正确的代码:

代码语言:javascript
复制
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;
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65325060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档