0

有没有办法使用 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) {});
4

2 回答 2

4

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

于 2018-06-16T13:25:34.970 回答
1

我正在阅读 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;
  }
于 2018-06-27T07:51:31.383 回答