我使用 Firestore 和 Flutter 进行聊天应用程序。它工作正常,但我看到了这个问题。有时消息不按顺序显示。例如,通常它们会被排序,最新的位于底部。但我在 iOS 和 Android 模拟器上进行测试,发现有时消息未按顺序显示。例如,我在 iOS 上发送消息,一切正常(按顺序)。然后我在不同的模拟器(例如 Android)上发送,消息显示在顶部,然后开始降序(在 iOS 上发送的消息的顶部)。
这是我的代码:
child: new FirestoreAnimatedList(
query: reference
.orderBy('timestamp', descending: true)
.snapshots(),
padding: new EdgeInsets.all(8.0),
reverse: true,
itemBuilder: (_, DocumentSnapshot snapshot,
Animation<double> animation, int x) {
return new Chat(
snapshot: snapshot, animation: animation);
},
),
'时间戳': DateTime.now(),
我已经尝试过这个但同样的问题:
'timestamp': DateTime.now().millisecondsSinceEpoch.toString()
我寻找了数周的答案,但没有找到。有人可以帮忙吗?
最佳答案
您可能会遇到此问题,因为设备报告的时间不同。
要解决此问题,请使用服务器时间而不是本地时间。这是通过将 timestamp
字段设置为 FieldValue.serverTimestamp()
来完成的。
关于firebase - 聊天消息排序不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53561687/