[SOLVED] type ‘_JsonQuerySnapshot’ error occured while using firestore QuerySnapShots

Issue

This Content is from Stack Overflow. Question asked by MooNChilD Song

I think I got used to using DocumentSnapshot, without using any ordering or filtering.

Now I am making a query streamer using the code :

Stream<QuerySnapshot> postStream_left = FirebaseFirestore.instance.collection('post')
.where('topic',isEqualTo: received_id)
.where('side',isEqualTo:'left')
.orderBy('like')
.snapshots();

Stream<QuerySnapshot> postStream_right = FirebaseFirestore.instance.collection('post')
.where('topic',isEqualTo: received_id)
.where('side',isEqualTo:'right')
.orderBy('like').snapshots();

return StreamBuilder(stream:postStream_left,builder: (context,snapshot_l)
        {
          if(!snapshot_l.hasData){
            return CircularProgressIndicator();
          }
          return StreamBuilder(stream:postStream_right,builder: (context,snapshot_r)
          {
            if(!snapshot_r.hasData){
              return CircularProgressIndicator();
            }
            final documents_l = snapshot_l.data as Map<String,dynamic>;     // Here is where I am stuck
            final documents_r = snapshot_r.data as Map<String,dynamic>;
            print('afepwmcakodepwxa.ofpe,[wambotpmwa');
            print(documents_l);
            print(documents_r);
            return CircularProgressIndicator();
          }
          );
        }
        );

What I want to do is show the data of postStream_left and postStream_right in two listview at the same time. However I donot know how to parse the data of each QuerySnapshot, since they did not work with code that works for Documentsnapshot.

type '_JsonQuerySnapshot' is not a subtype of type 'Map<String, dynamic>' in type cast

Here is the error I got but flutter does not tell me how I should cast the data.

Please tell me what to do and thank you very much.



Solution

Answer was simple. Defining the type of streambuilder and snapshot.docs clearly solved the problem. I leave the answer for my own study and for someone suffering like me.

StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
                                  stream: FirebaseFirestore.instance.collection('post')
                                      .where('topic',isEqualTo: received_id).where('side',isEqualTo:'left')
                                      .orderBy('like').snapshots(),
                                    builder: (BuildContext context, snapshot){
                                      if (snapshot.data == null) {
                                        return Center(child: CircularProgressIndicator());
                                      }
                                      else{
                                        final List<QueryDocumentSnapshot<Map<String, dynamic>>> docs = snapshot.data!.docs;


This Question was asked in StackOverflow by MooNChilD Song and Answered by MooNChilD Song It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?