订购带有时间戳的聊天消息
Order chat messages with timestamps
这是一个聊天应用程序,我尝试使用时间戳对上传到 firestore db 的消息进行排序,以显示 listView 上的最后一条消息,但在我将 'timestamp' 字段添加到集合并调用 .orderBy(Timestamp.now().toDate().toString(), descending: true)
我看到空屏幕!,即使我在“网络上”的 firbaase firestore 控制台中看到消息
这里是当用户发送新消息时
TextButton(
onPressed: () {
//Implement send functionality.
messageEditController.clear();
_firestore.collection('messages').add({
'sender': _loggedInUser.email,
'text': messageText,
'timestamp': Timestamp.now().toString(),
});
},
MessagesStream class
class MessagesStream extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: _firestore
.collection('messages')
.orderBy(Timestamp.now().toDate().toString(), descending: true)
.snapshots(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(
backgroundColor: Colors.lightBlueAccent,
),
);
} else if (snapshot.hasData) {
final messages = snapshot.data.docs;
List<MessageBubble> messageBubbles = [];
for (var message in messages) {
final messageSender = message.data()['sender'];
final messageText = message.data()['text'];
final currentUser = _loggedInUser.email;
if (currentUser == messageSender) {
// mwssage from logged in user
}
// ignore: missing_return
final messageBubble = MessageBubble(
sender: messageSender,
text: messageText,
isMe: currentUser == messageSender,
);
messageBubbles.add(messageBubble);
}
return Expanded(
child: ListView(
padding: EdgeInsets.symmetric(vertical: 20, horizontal: 10),
children: messageBubbles,
),
);
} else if (snapshot.hasError) {
return Expanded(
child: Container(
child: Text(snapshot.hasError.toString()),
),
);
}
},
);
orderBy
不接受值但字段名称可以正常工作。
尝试将这部分代码更改为:
_firestore
.collection('messages')
.orderBy('timestamp', descending: true)
.snapshots()
我还建议像这样保存 timestamp
值:
TextButton(
onPressed: () {
//Implement send functionality.
messageEditController.clear();
_firestore.collection('messages').add({
'sender': _loggedInUser.email,
'text': messageText,
'timestamp': new DateTime.now(),
});
},
否则,您只需保存一个字符串,该字符串不会按您期望的时间戳排序。如果您仅将其保存为字符串,它将按字符串顺序排序,而不是按您希望的实际时间戳排序。
这是一个聊天应用程序,我尝试使用时间戳对上传到 firestore db 的消息进行排序,以显示 listView 上的最后一条消息,但在我将 'timestamp' 字段添加到集合并调用 .orderBy(Timestamp.now().toDate().toString(), descending: true)
我看到空屏幕!,即使我在“网络上”的 firbaase firestore 控制台中看到消息
这里是当用户发送新消息时
TextButton(
onPressed: () {
//Implement send functionality.
messageEditController.clear();
_firestore.collection('messages').add({
'sender': _loggedInUser.email,
'text': messageText,
'timestamp': Timestamp.now().toString(),
});
},
MessagesStream class
class MessagesStream extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: _firestore
.collection('messages')
.orderBy(Timestamp.now().toDate().toString(), descending: true)
.snapshots(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(
backgroundColor: Colors.lightBlueAccent,
),
);
} else if (snapshot.hasData) {
final messages = snapshot.data.docs;
List<MessageBubble> messageBubbles = [];
for (var message in messages) {
final messageSender = message.data()['sender'];
final messageText = message.data()['text'];
final currentUser = _loggedInUser.email;
if (currentUser == messageSender) {
// mwssage from logged in user
}
// ignore: missing_return
final messageBubble = MessageBubble(
sender: messageSender,
text: messageText,
isMe: currentUser == messageSender,
);
messageBubbles.add(messageBubble);
}
return Expanded(
child: ListView(
padding: EdgeInsets.symmetric(vertical: 20, horizontal: 10),
children: messageBubbles,
),
);
} else if (snapshot.hasError) {
return Expanded(
child: Container(
child: Text(snapshot.hasError.toString()),
),
);
}
},
);
orderBy
不接受值但字段名称可以正常工作。
尝试将这部分代码更改为:
_firestore
.collection('messages')
.orderBy('timestamp', descending: true)
.snapshots()
我还建议像这样保存 timestamp
值:
TextButton(
onPressed: () {
//Implement send functionality.
messageEditController.clear();
_firestore.collection('messages').add({
'sender': _loggedInUser.email,
'text': messageText,
'timestamp': new DateTime.now(),
});
},
否则,您只需保存一个字符串,该字符串不会按您期望的时间戳排序。如果您仅将其保存为字符串,它将按字符串顺序排序,而不是按您希望的实际时间戳排序。