firebase - 聊天消息排序不正确

标签 firebase flutter google-cloud-firestore flutter-layout flutter-animation

我使用 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() 来完成的。

Documentation .

关于firebase - 聊天消息排序不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53561687/

相关文章:

flutter - flutter 的阴影看起来很丑

android - 在 android studio 中如何从磁盘重新加载代码文件?

angular - 我收到错误 "Spread types may only be created from object types.ts(2698)"

android - 未添加到 arrayList 中的项目

swift - 获取当前用户发布的消息列表

typescript - Firebase 云功能 - 未处理的错误错误 : Update()

javascript - Firestore云功能。https.onRequest Request.url.split ("/")不起作用

Firebase CLI 更改托管目标

flutter - 传递参数时,在处理之前调用构建函数执行

ios - 聊天分页中遇到的问题