首页 > 解决方案 > 颤振:从火力基地获取文件

问题描述

我想带出模型集合的文档 id '1004'。我该怎么做?我只知道集合和文档 ID '1004'。

错误代码:

DocumentSnapshot _model =  await carCollectionRef
          .doc()
          .collection(USER_CAR_COLLECTION)
          .doc()
          .collection(MODEL_COLLECTION)
          .doc('1004')
          .get();

在此处输入图像描述

标签: firebasefluttergoogle-cloud-firestore

解决方案


不是从Firestore. 但这就是你得到它的方式。

  foo() async {
   Map<String, dynamic> myData;

     await carCollectionRef
      .get()
      .then((snapshot) => snapshot.docs.forEach((element) {
          if (element.exists) {
            carCollectionRef
                .doc(element.id)
                .collection('USER_CAR_COLLECTION')
                .get()
                .then((snapshot2) => snapshot2.docs.forEach((element2) {
                      if (element2.exists) {
                        carCollectionRef
                            .doc(element.id)
                            .collection('USER_CAR_COLLECTION')
                            .doc(element2.id)
                            .collection('MODEL_COLLECTION')
                            .doc('1004')
                            .get()
                            .then((value) => {
                                  if (value.exists) {myData = value.data()}
                                });
                      }
                    }));
          }
        }));

     print(myData);
  }

推荐阅读