如何使用 Flutter 获取 Firestore 中每个集合的子集合

Ami*_*oui 0 firebase flutter google-cloud-firestore

我正在构建一个任务管理应用程序,其中每个用户都有多个项目(集合),每个项目内部都有任务(集合),每个任务内部都有子任务。

我能够使用此代码检索所有项目:

User user = Provider.of<User>(context);
CollectionReference projects = Firestore.instance.collection('users').document(user.uid).collection("projects");
Run Code Online (Sandbox Code Playgroud)

但我的问题是如何从每个项目中获取所有任务和子任务(如果存在)并将它们列在 StreamBuilder 中?

这是我当前的代码:

Flexible(
            child: StreamBuilder<QuerySnapshot>(
              stream: projects.snapshots(),
              builder: (BuildContext context,
                  AsyncSnapshot<QuerySnapshot> snapshot) {
                if (snapshot.hasError) {
                  return Text('Something went wrong');
                }

                if (snapshot.connectionState == ConnectionState.waiting) {
                  return Text("Loading");
                }

                return new ListView(
                  children:
                      snapshot.data.documents.map((DocumentSnapshot document) {
                    return new ListTile(
                      title: new Text(document.data['project_name']),
                      subtitle: new Text(document.data['project_description']),
                    );
                  }).toList(),
                );
              },
            ),
          ),
Run Code Online (Sandbox Code Playgroud)

如果项目有任务,我想在项目描述下面显示它们,如果任务有子任务,我想在其父任务下面显示它们。

Dou*_*son 5

听起来您本质上是在问如何列出嵌套的子集合,这对于 Web 和移动客户端来说是不可能的。也可以看看:

此外,所有 Firestore 查询都很浅,这意味着它们一次只考虑一个集合中的文档。没有深度或递归查询。

考虑在您可以get()直接使用的文档中维护项目列表。您可以使用该列表直接查询每个子集合。


归档时间:

查看次数:

3883 次

最近记录:

4 年,9 月 前