问题
Here I am trying to get the data from the firebase database using firstore instance in streamBuilder.
- .collection('/message_data/friendA##friendB/message_list') :-this is my collection-document path from firebase
No error, just a blank page.
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.friendName),
),
body: Column(
children: <Widget>[
Flexible(
child: StreamBuilder(
stream: Firestore.instance
.collection('/message_data/friendA##friendB/message_list')
.snapshots(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasError) {
return Text('Error on chatView ${snapshot.error.toString()}');
}
if (snapshot.connectionState == ConnectionState.active) {
if (snapshot.hasData) {
if (snapshot.data.documents .length > 0) {
return ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (BuildContext context, int index) {
DocumentSnapshot _document = snapshot.data.documents[index];
return ChatMessage(
isFriend: _document['fromA'],
isNotPrevious:snapshot.data.documents.length - 1 == index,
message: _document['content'],
friendInitial: 'T',
avatarUrl:'https://avatarfiles.alphacoders.com/132/132399.jpg',
);
},
);
}
else{
return Text('No messages found in chat view length vala');
}
}
else{
return Text('No messages found in chat view hasdata');
}
}
else{
return CircularProgressIndicator();
}
},
)),
回答1:
I think you have some error in your snapshot, but you also have an error in how you access your firestore data here. You have to access the data property of your DocumentSnapshot in order to access the data.
DocumentSnapshot _document = snapshot.data.documents[index];
Map chatData = _document.data;
return ChatMessage(
isFriend: chatData['fromA'],
isNotPrevious:snapshot.data.documents.length - 1 == index,
message: chatData['content'],
friendInitial: 'T',
avatarUrl:'https://avatarfiles.alphacoders.com/132/132399.jpg',
);
来源:https://stackoverflow.com/questions/60142173/not-able-to-access-the-data-from-firebase-using-streambuilder-in-flutter