Flutter Firebase数据库错误的时间戳顺序 [英] Flutter Firebase Database wrong timestamp order

查看:84
本文介绍了Flutter Firebase数据库错误的时间戳顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将时间戳设置为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<String, Object>,该信息不再能够保存有关子节点顺序的信息.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆