Flutter:Firebase 实时数据库 orderByChild 对查询结果没有影响 [英] Flutter: Firebase Real-Time database orderByChild has no impact on query result

查看:19
本文介绍了Flutter:Firebase 实时数据库 orderByChild 对查询结果没有影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经像这样将这样的数据插入到 Firebase 实时数据库中:

I have inserted data like this into Firebase Real-Time Database like this way:

我像这样查询数据库:

final databaseReference = FirebaseDatabase.instance.reference();
databaseReference
    .child('orders')
    .orderByChild('date_slug')
    .limitToFirst(pageSize)
    .once()
    .then((snapshot) {
  firstPageItems = snapshot.value;

  if (firstPageItems != null) {
    List<dynamic> curretList = [];
    firstPageItems.forEach((orderId, orderData) {

      print ("date_slug " + orderData['date_slug'] + "


");
      curretList.add(orderData);
    });

    _list.addAll(curretList);
    _listController.sink.add(_list);
  }
});

然而,数据并没有像我预期的那样排序.请参阅下面的输出.

However, the data didn't come back as sorted as I expected. See the output below.

I/flutter (17125): date_slug 2020-04-20 15:52:13
I/flutter (17125): 
I/flutter (17125): 
I/flutter (17125): date_slug 2020-04-20 15:52:11
I/flutter (17125): 
I/flutter (17125): 
I/flutter (17125): date_slug 2020-04-20 15:52:10
I/flutter (17125): 
I/flutter (17125): 
I/flutter (17125): date_slug 2020-04-20 15:52:12

推荐答案

只要您调用 firstPageItems = snapshot.value,就会将结果转换为地图/字典.字典可以保存结果的键和值,但它没有结果的相对顺序.

As soon as you call firstPageItems = snapshot.value, you are converting the results into a map/dictionary. A dictionary can hold the keys and the values of the results, but it has no place for the relative order of the results.

为了保持结果的顺序,您需要观察 onChildAdded:

To maintain the order of the results, you'll want to either observe onChildAdded:

var query = databaseReference
    .child('orders')
    .orderByChild('date_slug')
    .limitToFirst(pageSize);
query.onChildAdded
    .forEach((event) => {
      print(event.snapshot.value)
    });

如果您需要知道查询的所有子节点何时都已处理完毕,可以向 value 事件添加一个额外的侦听器:

If you need to know when all child nodes of your query have been handled, you can add an additional listener to the value event:

query.once().then((snapshot) {
  print("Done loading all data for query");
});

添加这个额外的侦听器不会导致下载额外的数据,因为 Firebase 会在幕后进行重复数据删除.

Adding this additional listener does not result in downloading extra data, as Firebase deduplicates then behind the scenes.

或者,您可以使用 FirebaseList 来自 FlutterFire 库,它使用相同的 onChildAdded 和其他 onChild... 流来维护索引列表.

Alternatively, you can the FirebaseList class from the FlutterFire library, which uses that same onChildAdded and the other onChild... streams to maintain an indexed list.

一个使用这个类的例子:

An example of using this class:

list = FirebaseList(query: query, 
  onChildAdded: (pos, snapshot) {},
  onChildRemoved: (pos, snapshot) {},
  onChildChanged: (pos, snapshot) {},
  onChildMoved: (oldpos, newpos, snapshot) {},
  onValue: (snapshot) {
    for (var i=0; i < this.list.length; i++) {
      print('$i: ${list[i].value}');
    }
  }
);

如您所见,这使用列表的 onValue 流按顺序循环遍历子项.onChild... 方法是 FirebaseList 类所必需的,但我们在这里没有对它们做任何有意义的事情.

As you can see this uses the onValue stream of the list to loop over the children in order. The onChild... methods are needed for the FirebaseList class, but we don't do anything meaningful with them here.

这篇关于Flutter:Firebase 实时数据库 orderByChild 对查询结果没有影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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