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');
}
}),
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With