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

huangapple go评论69阅读模式
英文:

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

问题

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

错误信息:

由于接收器可能为 'null',无法无条件地访问属性 'documents'。

是否有解决这个错误或实现方法的其他方式?

英文:

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'.

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

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

答案1

得分: 2

We need to make change on the lines
builder: (context, snapshot)
to
builder: (context, AsyncSnapshot snapshot)
then use
snapshot.data as snapshot.data!.

英文:

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

答案2

得分: 0

Sure, here is the translated code:

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);
}
英文:
final messages = snapshot.data?.docs.reversed;

    List&lt;MessageBubble&gt; messageBubbles =[];
    for( var message in messages!){
      var data = message.data() as Map;
      final messageText = data[&#39;text&#39;];
      final messageSender = data[&#39;sender&#39;];

      final currentUser = loggedInUser?.email;


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

huangapple
  • 本文由 发表于 2023年3月10日 01:23:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75688028.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定