我正在将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()
我寻找了数周的答案,但没有找到。有人可以帮忙吗?
答案 0 :(得分:0)
您可能会遇到此问题,因为设备报告的时间不同。
要解决此问题,请使用服务器时间而不是本地时间。这是通过将您的timestamp
字段设置为FieldValue.serverTimestamp()
来完成的。