简体   繁体   English

Flutter 使用 collectionGroup 从 firestore 获取数据

[英]Flutter getting data from firestore using collectionGroup

Am creating an app which will display content based on user location.我正在创建一个应用程序,它将根据用户位置显示内容。 For this am using the Geoflutterfire package to query the data from firestore.为此,我使用 Geoflutterfire package 从 firestore 查询数据。 The app returns data as desired when I use a single document field.for example当我使用单个文档字段时,应用程序会根据需要返回数据。例如

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);
    });

However I need to stream all documents from the "allcontents" sub collection and to achieve this tried using collectionGroup like this但是我需要 stream 来自“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 如下所示

StreamBuilder(
            stream: stream,
            builder: (BuildContext context,
                AsyncSnapshot<List<DocumentSnapshot>> snapshots) {
              if (snapshots.connectionState == ConnectionState.active &&
                  snapshots.hasData) {
Do stuff}else {
                return Center(child: CircularProgressIndicator());

For the first case(single doc query) it is able to retrieve data(Do stuff) From firestore For the second case(collectionGroup) it only shows the circular progress indicator.对于第一种情况(单个文档查询),它能够从 firestore 检索数据(做东西) 对于第二种情况(collectionGroup),它只显示循环进度指示器。

The GeoFlutterFire 's collectionRef takes Query type as input, so your code looks good and should work. GeoFlutterFire的 collectionRef 将Query类型作为输入,因此您的代码看起来不错并且应该可以工作。 While handling the streambuilder result you have to check for error在处理streambuilder结果时,您必须检查错误

if (snapshots.connectionState == ConnectionState.error) { // Error state } if (snapshots.connectionState == ConnectionState.error) { // 错误 state }

and update the result in screen.并在屏幕上更新结果。 Now, for any state other than success its coded to show the progress indicator.现在,对于除成功之外的任何 state,其编码为显示进度指示器。

Update for Permission-denied Error:权限被拒绝错误的更新:

This may because of the firestore security rules.这可能是因为 Firestore 安全规则。 I recommend this Firestore Security Rule reference, so that you can write more secure rules that match your application needs.我推荐这个Firestore 安全规则参考,这样您就可以编写更安全的规则来满足您的应用程序需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM