为什么通过 StreamBuilder 从 Cloud Firestore 获取消息时无法访问 snapshot.data.documents?

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

提问人:Samik Pandit 提问时间:3/10/2023 最后编辑:Frank van PuffelenSamik Pandit 更新时间:4/6/2023 访问量:179

问:

通过 从 Cloud Firestore 获取消息时无法访问。snapshot.data.documentsStreamBuilder

法典:

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,
          ),
        );
      },
    );
  }
}

错误:

不能无条件访问属性“documents”,因为接收器可以是“null”。

enter image description here

有什么方法可以解决这个错误或任何其他方法可以成功实现我的方法?

flutter firebase dart 谷歌云火商店 flutter-streambuilder

评论


答:

2赞 Samik Pandit 3/10/2023 #1

我们需要在生产线上做出改变

builder: (context, snapshot)

builder: (context, AsyncSnapshot snapshot)

然后使用

snapshot.data as snapshot.data!.

法典:

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,
          ),
        );
      },
    );
  }
}

0赞 Anish132 4/6/2023 #2
final messages = snapshot.data?.docs.reversed;

    List<MessageBubble> messageBubbles =[];
    for( var message in messages!){
      var data = message.data() as Map;
      final messageText = data['text'];
      final messageSender = data['sender'];

      final currentUser = loggedInUser?.email;


      final messageBubble = MessageBubble(
          sender: messageSender,
          text: messageText,
          isME: currentUser == messageSender,
      );
      messageBubbles.add(messageBubble);