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

Issue

I am getting this error when trying to fetch user posts from Firestore with the StreamBuilder.

@override
Widget build(BuildContext context) {
return Column(
  children: <Widget>[
Expanded(
child: StreamBuilder(
      stream: postRef
          .where("bookmarks",
          arrayContains: FirebaseAuth.instance.currentUser.uid)
          .orderBy('timestamp', descending: true)
          .snapshots(),
      builder: (context, snapshot) {
if (snapshot.hasData) {
        return ListView.separated(
          itemCount: snapshot.data.docs.length,
          itemBuilder: (context, index) {
            internetChecker(context);
            Review posts = Review.fromJson(snapshot.data);
            return Padding(
              padding: const EdgeInsets.only(bottom: 10.0),
              child: Posts(post: posts),
            );},
          separatorBuilder: (context, index) {
            return Column(
              children: [
                if (index % 3 == 0 && index != 0)
                  AdWidget(ad: ad)
              ],);});
} else if (snapshot.hasError) {
  return Center(
    child:  Text(Languages.of(context).errorOcc),
  );
} else {
  return  Container();}},))],);//)]);
}

I tried casting the review posts as Map<String, dynamic>, but then the child: Posts(post: posts) gives the same error. I tried suggestions from the answers but that is a FutureBuilder, not StreamBuilder. I am new to this, so still very confused. Help appreciated very much!

Solution

Use data() on docs to access individual documents with the help of index:

Review.fromJson(snapshot.data.docs[index]!.data()!)

Answered By – Tirth Patel

Answer Checked By – Mary Flores (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *