【问题标题】:Flutter Firestore Nested QueryFlutter Firestore 嵌套查询
【发布时间】:2018-11-26 01:09:35
【问题描述】:

有没有办法使用 Flutter 读取 Firestore 中的嵌套集合?我有以下数据结构

Users:
 - userId:
     - name: value,
     - surname: value;
     - addresses:
         - addressId:
              - street:value,
              - city: value

我可以使用以下代码读取第一个集合:

Firestore.instance.collection("users").document("userId")
        .snapshots().listen((snapShot) {});

有没有办法让我从上面的代码中访问地址,或者我必须运行另一个直接获取地址的命令,如下所示:

Firestore.instance.collection("users").document("userId").
        collections("addresses").snapshots().listen((snapShot) {});

【问题讨论】:

  • Firestore.instance .collection('abc') .document("aa") 。 collection('xyz') .getDocuments() .then((QuerySnapshot snapShot) { snapShot.documents.forEach((doc) { print('${doc.documentID}'); print(('${ei.data[ '您的字段名称']}')); }); });

标签: dart google-cloud-firestore flutter


【解决方案1】:

从 Firestore 读取文档不会自动从该文档的子集合中读取数据。如果您想要子集合中的数据,则需要为此进行额外的阅读。

【讨论】:

    【解决方案2】:

    我正在读取 Firestore 中的嵌套集合以填充扩展小部件。在https://stackoverflow.com/a/51057195/5013735 中查看我的解决方案

    诀窍在于创建如下结构:

      List<Widget> _getChildren() {
        List<Widget> children = [];
        documents.forEach((doc) {
          children.add(
            ProjectsExpansionTile(
              name: doc['name'],
              projectKey: doc.documentID,
              firestore: firestore,
            ),
          );
        });
        return children;
      }
    

    【讨论】:

      猜你喜欢
      • 2019-07-18
      • 2018-10-16
      • 1970-01-01
      • 2021-09-29
      • 2020-02-03
      • 1970-01-01
      • 1970-01-01
      • 2018-05-23
      • 2022-12-01
      相关资源
      最近更新 更多