首页 > 解决方案 > 聊天消息排序不正确

问题描述

我将 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()

我寻找几周的答案,但没有找到。任何人都可以帮忙吗?

标签: firebasefluttergoogle-cloud-firestoreflutter-layoutflutter-animation

解决方案


您可能会遇到此问题,因为设备报告的时间不同。

要解决此问题,请使用服务器时间而不是本地时间。这是通过将您的timestamp字段设置为 来完成的FieldValue.serverTimestamp()

文档


推荐阅读