Reputation: 41
When I'm fetching the messages from fire base new messages are appears randomly in the list view how I can set the new messages on the bottom of my list view in flutter?
Here is the code:
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
StreamBuilder<QuerySnapshot>(
stream: _firestore.collection('messages').snapshots(),
builder: (context, snapshot) {
List<MyMessageContainer> messageWidget = [];
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(
backgroundColor: Colors.lightBlue,
),
);
}
final messagesDoc = snapshot.data.docs.reversed;
for (var messageData in messagesDoc) {
final messageBody = messageData.data();
final messageText = messageBody['text'];
final messageSender = messageBody['sender'];
final msg = MyMessageContainer(
message: messageText,
sender: messageSender,
isCurrentUser: messageSender == user.email ? true : false,
);
messageWidget.add(msg);
}
return Expanded(
child: ListView(
reverse: true,
children: messageWidget,
),
);
},
),
Upvotes: 0
Views: 62
Reputation: 598765
Firestore has no built-in way to identify "new" messages. If you want that, you'll have to store a value in each document with a timestamp.
When you've done that, you can get them in the correct order in your code with:
_firestore.collection('messages').orderBy("timestampField").snapshots()
Upvotes: 1