Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert 'QueryDocumentSnapshot' to Json

I'm trying to use streambuilder to receive data and turn this data to json for my model but getting error below; So my question is how can i convert stream data from firebase to json

Error

type 'QueryDocumentSnapshot' is not a subtype of type 'Map<dynamic, dynamic>'

FromJson method of model

factory Review.fromJson(Map<dynamic, dynamic> json) => Review(
     //Bunch of random fields here);

StreamBuilder

 StreamBuilder<Object>(
                              stream: widget.reviewStream,
                              builder: (context, AsyncSnapshot snapshots) {
                                if (snapshots.hasData) {
                                  var data = snapshots.data.docs;
                                  return ListView.builder(
                                    shrinkWrap: true,
                                    itemCount: data.length,
                                    itemBuilder:
                                        (BuildContext context, int index) {
                                      return Padding(
                                        padding: const EdgeInsets.symmetric(
                                            vertical: 12.0),
                                        child: Padding(
                                            padding:
                                                const EdgeInsets.all(8.0),
                                            child: ReviewBox(
                                              review: Review.fromJson(
                                                  data[index]),
                                              user: widget.user,
                                            )),
                                      );
                                    },
                                  );
                                } else {
                                  return Text('No Data');
                                }
                              }),
like image 962
Emir Kutlugün Avatar asked Oct 14 '25 08:10

Emir Kutlugün


1 Answers

You need to call data() method on data[index] to get Map<String, dynamic>, because the data[index] is a QueryDocumentSnapshot.

Review.fromJson(data[index].data());
like image 94
Tirth Patel Avatar answered Oct 17 '25 01:10

Tirth Patel