firebase - 聊天消息排序不正确
问题描述
我将 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);
},
),
'timestamp': DateTime.now(),
我试过这个但同样的问题:
'timestamp': DateTime.now().millisecondsSinceEpoch.toString()
我寻找几周的答案,但没有找到。任何人都可以帮忙吗?
解决方案
您可能会遇到此问题,因为设备报告的时间不同。
要解决此问题,请使用服务器时间而不是本地时间。这是通过将您的timestamp
字段设置为 来完成的FieldValue.serverTimestamp()
。
文档。
推荐阅读
- sql-server - 关键字 'key' pyodbc 错误 42000 附近的语法不正确
- apache-flink - 在 ProcessWindowFunction 上实现 onTimer 函数
- sql - 如何将年份(带逗号)转换为年、月和日
- h2 - Rundeck H2 数据库清理
- css - 反应组件中的CSS样式
- amazon-web-services - aws cloudformation 包将一些本地路径转换为 S3 uris,但不是全部
- sql-server - 如何使用静态端口在 SQL Server 2019 Express 上侦听 localhost?
- angular - Angular 10:在实时服务器上部署 Angular 应用程序
- javascript - 当我将跨域设置为匿名时,无法加载图像(操作被 cors 策略阻止)
- javascript - 更改组件角度时丢失脚本