flutterfirebasedartgoogle-cloud-firestoreflutter-streambuilder

Why the snapshot.data.documents can't be accessed while getting messages from Cloud Firestore through StreamBuilder?


The snapshot.data.documents can't be accessed while getting messages from Cloud Firestore through StreamBuilder.

Code:

class MessagesStream extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: _firestore.collection('messages').snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return Center(
            child: CircularProgressIndicator(
              backgroundColor: Colors.lightBlueAccent,
            ),
          );
        }
        final messages = snapshot.data.documents.reversed;
        List<MessageBubble> messageBubbles = [];
        for (var message in messages) {
          final messageText = message.data['text'];
          final messageSender = message.data['sender'];

          final currentUser = loggedInUser;

          final messageBubble = MessageBubble(
            sender: messageSender,
            text: messageText,
            isMe: currentUser == messageSender,
          );

          messageBubbles.add(messageBubble);
        }
        return Expanded(
          child: ListView(
            reverse: true,
            padding:
                const EdgeInsets.symmetric(horizontal: 10.0, vertical: 20.0),
            children: messageBubbles,
          ),
        );
      },
    );
  }
}

Error:

The property 'documents' can't be unconditionally accessed because the receiver can be 'null'.

enter image description here

Is there any way to solve this bug or any other way in which my method can be successfully implemented ?


Solution

  • We need to make change on the lines

    builder: (context, snapshot)
    

    to

    builder: (context, AsyncSnapshot snapshot)
    

    then use

    snapshot.data as snapshot.data!.
    

    Code:

    class MessagesStream extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return StreamBuilder<QuerySnapshot>(
          stream: _firestore.collection('messages').snapshots(),
          builder: (context, AsyncSnapshot snapshot) {
            if (!snapshot.hasData) {
              return const Center(
                child: CircularProgressIndicator(
                  backgroundColor: Colors.lightBlueAccent,
                ),
              );
            }
            final messages = snapshot.data!.docs;
            List<MessageBubble> messageBubbles = [];
            for (var message in messages) {
              final messageText = message['text'];
              final messageSender = message['sender'];
    
              final messageBubble = MessageBubble(
                sender: messageSender,
                text: messageText,
                isMe: loggedInUser.email == messageSender,
              );
              messageBubbles.add(messageBubble);
            }
            return Expanded(
              child: ListView(
                reverse: true,
                padding:
                    const EdgeInsets.symmetric(horizontal: 10.0, vertical: 20.0),
                children: messageBubbles,
              ),
            );
          },
        );
      }
    }