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