【问题标题】:Chat messages not ordering correctly聊天消息排序不正确
【发布时间】:2019-05-02 20:30:12
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

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

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

    Documentation.

    【讨论】:

    • 感谢您的回复!我现在试试
    • @FlutterFirebase 运气好吗?
    • @FlutterFirebase 没问题!如果是,请确保将您的问题标记为已回答:)
    • 我也遇到过这个排序问题试过你的答案但没有运气,请帮助
    猜你喜欢
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-04
    • 2022-06-29
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    相关资源
    最近更新 更多