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

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

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

发布评论

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

>www.elefans.com

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