【问题标题】:flutter loop through firebase node that consists of singlenodes and nodes with children颤振循环通过由单节点和具有子节点的节点组成的firebase节点
【发布时间】:2018-10-12 20:48:31
【问题描述】:

我有这段代码:

  routineExersiceIDsetter() async{
    String routineId;
     final response = await reference.child(_userId).once();
     response.value.forEach((k,v){
       if(v["name"] == "First Routine"){
         currentRoutineId = k;
         routineId = k;
       }
     });
     final response2 = await reference.child(_userId).child(currentRoutineId).once();
     response2.value.forEach((k, v){
         if(v["name"] == "Day 1"){
           currentSchemeId = k;
       }

     });
  }

还有这个 firebase 数据库

{
  "-LBNflaqFEb4DtZiYxnh" : {
    "-LBWa99g4yCstUBkDGa7" : {
      "name" : "Day 1"
    },
    "name" : "First Routine"
  }
}

第一行 forEach 有效,但第二行无效。有没有办法让这项工作发挥作用,还是我需要更改我的 Firebase 架构?

【问题讨论】:

  • 不需要把这两个请求串起来吗?对 Firebase 的查询是异步的,因此您应该等待第一个查询解析为具有 currentRoutineId 的值,然后才能执行第二个查询。我不知道如何在飞镖中链接它,所以我无法为您提供完整的解决方案。
  • @RenaudTarnec 我以为我已经用 await 请求做到了,我得到的错误是:type 'String' is not a subtype of type 'int' of 'index'
  • 我不知道飞镖,但我感觉你没有将这两个 awaits 联系起来。你确定currentRoutineId 在第二个“等待”中有正确的值吗?你可以用控制台或日志检查吗?
  • @RenaudTarnec 从我看到的情况来看,它得到了正确的routineid,但它仍然在第二个 foreach 中给出错误
  • 你到底想做什么?因为似乎所有信息都在一个节点下

标签: json firebase dart flutter


【解决方案1】:

我意识到应该将第二个 if 语句包装在另一个 if 语句中:

if(k!="name"){
   if(v["name"] == "Day 1"){
     currentSchemeId =k;
   }
}

【讨论】:

    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多