Flutter:Firebase实时数据库orderByChild对查询结果没有影响

编程入门 行业动态 更新时间:2024-10-23 07:15:16
本文介绍了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对查询结果没有影响

本文发布于:2023-10-25 22:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528251.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:查询结果   实时   数据库   Flutter   Firebase

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!