Flutter Firebase 数据库错误的时间戳顺序 [英] Flutter Firebase Database wrong timestamp order
问题描述
我正在尝试将时间戳设置到 firebase 实时数据库中,但是当我检索时,不是按时间戳排序.我喜欢这样.
I'm trying to set timestamp into firebase realtime database but when I retrieve, not ordering by timestamp. I did like so.
FirebaseDatabase.instance.reference().child('path').push().set({
'timestamp': ServerValue.timestamp
});
这是节点
然后我就这样检索.
FirebaseDatabase.instance.reference().child('path').orderByChild('timestamp').once().then((snap) {
print(snap.value);
});
但输出是这个
{-LJhyfmrWVDD2ZgJdfMR: {timestamp: 1534074731794}, -LJhyWVi6LddGwVye48K: {timestamp: 1534074689667}, -LJhzDlvEMunxBpRmTkI: {timestamp: 1534074875091}
那些不是按时间戳排序的.我错过了什么吗?否则这是 firebase 错误还是抖动?
Those are not ordered by timestamp. Am I missing something? Otherwise is this firebase error or flutter?
推荐答案
数据以正确的顺序检索到 DataSnapshot
.但是当您调用 snap.value
时,快照中的信息必须转换为 Map
,它不再能够保存有关子节点.
The data is retrieved in the right order into a DataSnapshot
. But when you call snap.value
the information from the snapshot has to be converted into a Map<String, Object>
, which not longer can hold information about the order of the child nodes.
为了保持顺序,您必须使用循环处理DataSnapshot
中的子节点.我不是 Flutter 的专家(根本),但我无法快速找到一种方法来为价值事件做到这一点.所以你可能想改为监听 .childAdded
:
To maintain the order, you have to process the child nodes from the DataSnapshot
with a loop. I'm not an expert in Flutter (at all), but I can't quickly find a way to do this for a value event. So you might want to instead listen for .childAdded
:
FirebaseDatabase.instance
.reference()
.child('path')
.orderByChild('timestamp')
.onChildAdded
.listen((Event event) {
print(event.snapshot.value);
})
这篇关于Flutter Firebase 数据库错误的时间戳顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!