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

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

问题描述

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

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

然后我像这样查询数据库:

And I query the database back 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'] + "\r\n\r\n\r\n");
      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.

使用此类的示例:

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流按顺序循环遍历子级. FirebaseList类需要onChild...方法,但是在这里我们对它们没有做任何有意义的事情.

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天全站免登陆