Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

geofirestore - 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. 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 = 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());

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The GeoFlutterFire's collectionRef takes Query type as input, so your code looks good and should work. While handling the streambuilder result you have to check for error

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

and update the result in screen. Now, for any state other than success its coded to show the progress indicator.

Update for Permission-denied Error:

This may because of the firestore security rules. I recommend this Firestore Security Rule reference, so that you can write more secure rules that match your application needs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...