如何在 FirebaseFirestore、Flutter 中迭代集合、文档和子集合

时间:2021-05-31 11:55:26

标签: firebase flutter google-cloud-firestore

我需要从日期 (22, 25, 26, 27, ....) 获取所有文档的 ID

dailyWinner/2021-05/26 >>> 所有文档 ID

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

Firestore 中的查询很浅,这意味着当您获取文档时,您无法取回任何链接子集合中包含的数据。

因此您需要获取 2021-05 文档的子集合列表,并且对于每个子集合,查询其包含的所有文档。

你会遇到的难点是“retrieving a list of collections is not possible with the mobile/web client libraries”(包括 Flutter 插件)。

您将在此 article 中找到两种列出文档子集合的方法(即,将集合 ID 列表保存在父文档中或使用 Callable Cloud Function)。获得子集合 ID 后,您需要查询每个子集合,例如:

FirebaseFirestore.instance
    .collection('dailyWinners/2021-05/22')
    .get()
    .then((QuerySnapshot querySnapshot) {
        querySnapshot.docs.forEach((doc) {
            // ...
        });
    });