Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Firebase检索子集合的嵌套forEach_Firebase_Flutter_Dart_Google Cloud Firestore - Fatal编程技术网

从Firebase检索子集合的嵌套forEach

从Firebase检索子集合的嵌套forEach,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,我正在尝试使用嵌套的foreach检索多个子集合以添加到列表中: List<Widget> TaskList() { List<Widget> lines = []; firestoreInstance.collection("tasks").where("type", isEqualTo: "urgent").getDocuments().then((querySnapshot) { querySnapshot.documents.for

我正在尝试使用嵌套的foreach检索多个子集合以添加到列表中:

List<Widget> TaskList() {
    List<Widget> lines = [];
    firestoreInstance.collection("tasks").where("type", isEqualTo: "urgent").getDocuments().then((querySnapshot) {
        querySnapshot.documents.forEach((result) {
            firestoreInstance.collection("tasks").document(result.documentID).collection("todo").getDocuments().then((querySnapshot) {
                querySnapshot.documents.forEach((result) {
                    lines.add(Row(
                    children: <Widget> [
                    Expanded(flex: 7, child: Text("test", style: TextStyle(fontSize: 20))), Expanded(flex: 1, child: Text("test", style: TextStyle(fontSize: 20))), Expanded(flex: 2, child: Text("test", style: TextStyle(fontSize: 20), textAlign: TextAlign.right)), ], ));
                });
            });
        });
    });
    return lines;
}
List TaskList(){
列表行=[];
firestoreInstance.collection(“tasks”).where(“type”,isEqualTo:“emergency”).getDocuments().then((querySnapshot){
querySnapshot.documents.forEach((结果){
firestoreInstance.collection(“tasks”).document(result.documentID)。collection(“todo”).getDocuments()。然后((querySnapshot){
querySnapshot.documents.forEach((结果){
行。添加(第行)(
儿童:[
扩展(flex:7,子项:文本(“测试”,样式:TextStyle(fontSize:20))),扩展(flex:1,子项:文本(“测试”,样式:TextStyle(fontSize:20))),扩展(flex:2,子项:文本(“测试”,样式:TextStyle(fontSize:20),textAlign:textAlign.right)),);
});
});
});
});
回流线;
}
上面的代码可以运行,但列表为空。我认为这与等待查询完成有关,因为如果我在不访问Firebase的情况下将forEach替换为simple for循环,它就会工作(添加到列表中的行)。我知道我必须以某种方式利用未来。但是我不能真正为这个嵌套的foreach场景设计出语法


感谢您的帮助或有用示例的提示。

您可以使用
async
wait

  Future<List<Widget>> TaskList() async {
    List<Widget> lines = [];
    QuerySnapshot result = await firestoreInstance
        .collection("tasks")
        .where("type", isEqualTo: "urgent")
        .getDocuments();
    for (var res in result.documents) {
      QuerySnapshot todoResult = await firestoreInstance
          .collection("tasks")
          .document(res.documentID)
          .collection("todo")
          .getDocuments();
      for (var todoRes in todoResult.documents) {
        lines.add(Row(
          children: <Widget>[
            Expanded(
                flex: 7, child: Text("test", style: TextStyle(fontSize: 20))),
            Expanded(
                flex: 1, child: Text("test", style: TextStyle(fontSize: 20))),
            Expanded(
                flex: 2,
                child: Text("test",
                    style: TextStyle(fontSize: 20),
                    textAlign: TextAlign.right)),
          ],
        ));
      }
    }
    return lines;
  }
}

并在
FutureBuilder

中使用上述函数使您的方法异步并使用usr FutureBuilder来构建它。非常感谢您的建议。我没有完全按照你的想法去做,但是通过理解你的想法,我成功了。我正在使用两个FutureBuilder(每个都有一个未来)。第一个FutureBuilder在for循环中为检索到的结果的每个成员调用TaskList()函数。另一个FutureBuilder使用收到的documentID检索子集合。有点凌乱,但很管用。我正在研究是否可以进一步简化它。
  Future<QuerySnapshot> TaskList() async {
    QuerySnapshot todoResult;
    QuerySnapshot result = await firestoreInstance
        .collection("tasks")
        .where("type", isEqualTo: "urgent")
        .getDocuments();
    for (var res in result.documents) {
       todoResult = await firestoreInstance
          .collection("tasks")
          .document(res.documentID)
          .collection("todo")
          .getDocuments();
    }
    return todoResult;
  }