英文:
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'.
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<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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论