草庐IT

firebase-realtime-database - Flutter Firebase 数据库错误的时间戳顺序

coder 2023-07-23 原文

我正在尝试将时间戳设置到 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 错误还是 flutter?

最佳答案

数据以正确的顺序检索到 DataSnapshot 中.但是当你调用snap.value快照中的信息必须转换为 Map<String, Object> ,它不再可以保存有关子节点顺序的信息。

要维护顺序,您必须处理来自 DataSnapshot 的子节点用一个循环。我(根本不是 Flutter 方面的专家),但我无法快速找到一种方法来为值(value)事件做到这一点。所以你可能想改为监听 .childAdded :

   FirebaseDatabase.instance
        .reference()
        .child('path')
        .orderByChild('timestamp')
        .onChildAdded
        .listen((Event event) {
      print(event.snapshot.value);
    })

关于firebase-realtime-database - Flutter Firebase 数据库错误的时间戳顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51808719/

有关firebase-realtime-database - Flutter Firebase 数据库错误的时间戳顺序的更多相关文章

随机推荐