【问题标题】:Flutter - Fetching a collection from firestore that has multiple sub-collectionsFlutter - 从具有多个子集合的 Firestore 中获取集合
【发布时间】:2026-02-04 23:10:01
【问题描述】:

有什么方法可以使用 Flutter 从 Firebase Firestore 中检索集合及其子集合?

fetchTestSite() async {
    return await FirebaseFirestore.instance
        .collection('sites')
        .doc('4R3aOMBFTjumYCbETDU8')
        .get()
        .then((DocumentSnapshot doc) {
            print('document: ${doc.data()}');
        });
}

这段代码sn-p只返回主集合,没有现有的子集合

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    有没有办法使用 Flutter 从 Firebase Firestore 中检索集合及其子集合?

    不,你无法做到这一点。 Firestore 中的查询很浅。这意味着它只能从运行查询的集合中返回文档。 无法在单个查询中从*集合和子集合中获取文档。您只能从单个集合中获取文档。

    但是,可以进行“破解”。如果要从特定文档对应的子集合中获取数据,可以使用collectionGroup来获取某个路径下的文档。我在下面的文章中解释过:

    您如何做到这一点以及您可能有哪些限制。

    如果您考虑在某个时间点尝试使用Firebase Realtime Database,您正在寻找它是可能的,因为当您在特定节点上附加一个侦听器时,您会下载它下面的所有数据。

    【讨论】: