Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.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
需要Flatter Firebase嵌套集合帮助_Firebase_Flutter_Google Cloud Firestore_Collections_Nested - Fatal编程技术网

需要Flatter Firebase嵌套集合帮助

需要Flatter Firebase嵌套集合帮助,firebase,flutter,google-cloud-firestore,collections,nested,Firebase,Flutter,Google Cloud Firestore,Collections,Nested,我已经创建了一个带有嵌套集合的FireStore,现在我想根据一些条件从中获取数据。我尝试创建一个“未来”,尝试从集合中获取数据并存储在其中,然后在“FutureBuilder”中返回。但由于某些原因,我的代码正在工作,但没有显示任何输出 我的数据库结构 Class Documents --------------- course (collection) Doc

我已经创建了一个带有嵌套集合的FireStore,现在我想根据一些条件从中获取数据。我尝试创建一个“未来”,尝试从集合中获取数据并存储在其中,然后在“FutureBuilder”中返回。但由于某些原因,我的代码正在工作,但没有显示任何输出

我的数据库结构

Class
     Documents
             ---------------
             course (collection)       
                             Documents
                                     CourseName                                   
                                     Slot
                                     TeacherId
             ---------------
             ClassId
             ClassName
从集合中获取数据的代码

Future<List<Teaching>> findTeachingCourses(String documnentId) async {
    Future<List<Teaching>> teachingList = Future<List<Teaching>>.delayed(
      Duration(seconds: 0),
      () {
        Future<QuerySnapshot> result = classCollection.getDocuments();
        List<Teaching> list = new List<Teaching>();

        result.then(
          (value) => value.documents.forEach(
            (element) {
              Future<QuerySnapshot> result2 = classCollection
                  .document(element.documentID)
                  .collection("course")
                  .getDocuments();

              result2.then(
                (value2) => value2.documents.forEach(
                  (element2) {
                    //print(element.data);
                    //print(element2.data);

                    Stream<DocumentSnapshot> result3 =
                        collection.document(documnentId).get().asStream();

                    result3.forEach((element3) {
                      if (element3.data["id"] == element2.data["teacherId"]) {
                        Courses course = Courses(element2.data["courseName"],
                            element2.data["slot"], element2.data["teacherId"]);

                        Teaching teaching =
                            Teaching(course, element.data["classid"]);
                        list.add(teaching);

                        print(course.toJson());
                        print(element3.data["id"]);
                      }
                    });
                  },
                ),
              );
            },
          ),
        );
        return list;
      },
    );

    return teachingList;
  }
Future-findTeachingCourses(字符串documnentd)异步{
未来教学英语(
持续时间(秒:0),
() {
未来结果=classCollection.getDocuments();
列表=新列表();
结果呢(
(value)=>value.documents.forEach(
(要素){
未来结果2=类集合
.document(元素.documentID)
.收集(“课程”)
.getDocuments();
结果2.然后(
(value2)=>value2.documents.forEach(
(要素2){
//打印(元素数据);
//打印(元素2.数据);
流结果3=
collection.document(documnentd.get().asStream();
结果3.forEach((元素3){
if(element3.data[“id”]==element2.data[“teacherId”]){
课程=课程(element2.data[“courseName”],
element2.data[“slot”]、element2.data[“teacherId”];
教学=
教学(课程、元素、数据[“classid”);
增加(教学);
打印(course.toJson());
打印(元素3.数据[“id]”);
}
});
},
),
);
},
),
);
退货清单;
},
);
回归教学英语;
}
生成器代码

child: FutureBuilder<List<Teaching>>(
          future: repository.findTeachingCourses(_CurrentUser.documentId),
          builder: (context, snapshot) {
            if (snapshot.connectionState != ConnectionState.done) {
              //return showLoaderDialog(context);
              print("show loading dialog");
            }

            List<Teaching> list = snapshot.data ?? [];

            return ListView.builder(
              itemCount: list.length,
              itemBuilder: (context, index) {
                Teaching teaching = list[index];
                return Card(
                  child: Padding(
                    padding: const EdgeInsets.all(8),
                    child: Column(
                      children: <Widget>[
                        Text("Class ID : " + teaching.classid.toString()),
                        Text("Course : " + teaching.course.courseName),
                      ],
                    ),
                  ),
                );
              },
            );
          },
        ),
child:FutureBuilder(
未来:repository.findTeachingCourses(_CurrentUser.documentId),
生成器:(上下文,快照){
if(snapshot.connectionState!=connectionState.done){
//返回showLoaderDialog(上下文);
打印(“显示加载对话框”);
}
列表=snapshot.data???[];
返回ListView.builder(
itemCount:list.length,
itemBuilder:(上下文,索引){
教学=列表[索引];
回程卡(
孩子:填充(
填充:常量边集。全部(8),
子:列(
儿童:[
文本(“类ID:+teaching.classid.toString()),
文本(“课程:+教学。课程。课程名称),
],
),
),
);
},
);
},
),

代码运行时没有给我任何错误,但我没有得到任何输出。有什么建议吗?

这根本不是一个完整的解决方案,但希望能让您了解如何组织代码

基本上,您可以使用wait而不是then来删除被包装到多个括号层中的代码。你也应该考虑给变量更多有意义的名字! 下面是我重写代码的尝试,但在发现不知道代码中的
集合
变量后,我未能完成

 Future<List<Teaching>> findTeachingCourses(String documnentId) async {
    // This would be result
    final QuerySnapshot classesSnapshot = await classCollection.get();

    // This would be result2 combined in a single List
    final List<Future<QuerySnapshot>> coursesFuture = classesSnapshot.docs
        .map((classDoc) =>
            classCollection.doc(classDoc.id).collection("course").get())
        .toList();

    // This would be element2 combined in a single List
    final List<QuerySnapshot> coursesSnapshotList =
        await Future.wait(coursesFuture);

    // This would be element3
    final DocumentSnapshot targetSnapshot =
        await classCollection.doc(documnentId).get();
    }
Future-findTeachingCourses(字符串documnentd)异步{
//这就是结果
final QuerySnapshot classesSnapshot=await classCollection.get();
//这将被合并到一个列表中
最终列表coursesFuture=classesSnapshot.docs
.map((classDoc)=>
classCollection.doc(classDoc.id).collection(“课程”).get())
.toList();
//这将是元素2合并在一个列表中
最终列表课程快照列表=
等待未来;
//这将是元素3
最终文档快照targetSnapshot=
等待classCollection.doc(documnentd.get();
}

这根本不是一个完整的解决方案,但希望能让您了解如何组织代码

基本上,您可以使用wait而不是then来删除被包装到多个括号层中的代码。你也应该考虑给变量更多有意义的名字! 下面是我重写代码的尝试,但在发现不知道代码中的
集合
变量后,我未能完成

 Future<List<Teaching>> findTeachingCourses(String documnentId) async {
    // This would be result
    final QuerySnapshot classesSnapshot = await classCollection.get();

    // This would be result2 combined in a single List
    final List<Future<QuerySnapshot>> coursesFuture = classesSnapshot.docs
        .map((classDoc) =>
            classCollection.doc(classDoc.id).collection("course").get())
        .toList();

    // This would be element2 combined in a single List
    final List<QuerySnapshot> coursesSnapshotList =
        await Future.wait(coursesFuture);

    // This would be element3
    final DocumentSnapshot targetSnapshot =
        await classCollection.doc(documnentId).get();
    }
Future-findTeachingCourses(字符串documnentd)异步{
//这就是结果
final QuerySnapshot classesSnapshot=await classCollection.get();
//这将被合并到一个列表中
最终列表coursesFuture=classesSnapshot.docs
.map((classDoc)=>
classCollection.doc(classDoc.id).collection(“课程”).get())
.toList();
//这将是元素2合并在一个列表中
最终列表课程快照列表=
等待未来;
//这将是元素3
最终文档快照targetSnapshot=
等待classCollection.doc(documnentd.get();
}

你能用文字解释一下你想做什么吗?另外,也许你可以用wait而不是then来清理你的代码!它们实际上是相同的东西(差不多)@dshukertjr抱歉,兄弟,我忘了解释我想要达到的目标,我实际上想要得到当前用户正在教授的课程。我有class_集合和user_集合,第二个集合是user_集合,我在这里比较course.teacherId和userId。你能用语言解释一下你想做什么吗