Flutter: Migrating to null safety leads to errors

49 Views Asked by At

After migrating to null safety:dataSnapshot.data.docs; .docs is underlined red. I have tried dataSnapshot.data?.docs, dataSnapshot.data!.docs but still underlined red is there.

   StreamBuilder(
      stream: commentsReference
          .doc(postId)
          .collection("comments")
          .orderBy("timestamp", descending: false)
          .snapshots(),
      builder: (context, dataSnapshot) {
        if (!dataSnapshot.hasData) {
          return loader();
        }
      
        List<CommentTile> comments = [];
        dataSnapshot.data.docs.forEach((doc) {//.docs is underlined red
        //tried dataSnapshot.data?.docs, dataSnapshot.data!.docs.

          comments.add(CommentTile.fromDocument(doc));
        });
        return SingleChildScrollView(
          child: Column(children: comments),
        );
      },
    );
1

There are 1 best solutions below

0
On

Maybe you have model like this (or must be)

class Post {
 //title
 //contents
 List<CommentTile> docs;
}

and you have to change this

StreamBuilder -> StreamBuilder<Post>