我正在尝试将时间戳设置为firebase实时数据库,但是当我检索时,不按时间戳排序。我确实喜欢这样。
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错误还是颤动?
数据以正确的顺序检索到DataSnapshot
。但是当你调用snap.value
时,快照中的信息必须转换为Map<String, Object>
,它不再能保存有关子节点顺序的信息。
要维护顺序,必须使用循环处理DataSnapshot
中的子节点。我不是Flutter的专家(根本没有),但是我无法快速找到为价值事件做这件事的方法。所以你可能想听听.childAdded
:
FirebaseDatabase.instance
.reference()
.child('path')
.orderByChild('timestamp')
.onChildAdded
.listen((Event event) {
print(event.snapshot.value);
})