Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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');
                                }
                              }),
question from:https://stackoverflow.com/questions/66059444/convert-querydocumentsnapshot-to-json

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...