首页 > 解决方案 > 在 Flutter 中使用 collectionGroup 从 Firestore 中检索数据

问题描述

我正在创建一个应用程序,它将根据用户位置显示内容。为此,我使用 Geoflutterfire 包从 firestore 查询数据。当我使用单个文档字段时,应用程序会根据需要返回数据。例如

stream = radius.switchMap((rad) {
      var collectionReference = firestore
          .collection("content")
         .doc("onecontentid")
         .collection("allcontents");
      return geo.collection(collectionRef: collectionReference).within(
          center: center, radius: rad, field: 'position', strictMode: true);
    });

但是我需要从“allcontents”子集合中流式传输所有文档,并尝试使用这样的collectionGroup来实现这一点

stream = radius.switchMap((rad) {
          var collectionReference = firestore
              
             .collectionGroup("allcontents");
          return geo.collection(collectionRef: collectionReference).within(
              center: center, radius: rad, field: 'position', strictMode: true);
        });

which does not return any data.

My streambuilder for displaying the data looks like this
StreamBuilder(
            stream: stream,
            builder: (BuildContext context,
                AsyncSnapshot<List<DocumentSnapshot>> snapshots) {
              if (snapshots.connectionState == ConnectionState.active &&
                  snapshots.hasData) {
Do stuff}else {
                return Center(child: CircularProgressIndicator());

对于第一种情况(单个文档查询),它能够从 firestore 检索数据(Do stuff) 对于第二种情况(collectionGroup),它只显示循环进度指示器。

标签: fluttergoogle-cloud-firestore

解决方案


您可以在 GitHub 上尝试对我的问题的这个答案。


推荐阅读