Flutter: Firebase Real-Time database orderByChild has no impact on query result

半世苍凉 提交于 2020-04-29 03:51:03

问题


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

回答1:


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.

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

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

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:

var ref = databaseReference
  .child('orders')
  .orderByChild('date_slug')
  .limitToFirst(pageSize)
list = FirebaseList(query: ref, 
  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}');
    }
  }
);

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.



来源:https://stackoverflow.com/questions/61333194/flutter-firebase-real-time-database-orderbychild-has-no-impact-on-query-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!