简体   繁体   中英

Flutter/Firebase Realtime database: how to use equalsTo?

My db structure looks like this:

在此处输入图像描述

final databaseReference = FirebaseDatabase.instance.reference();
databaseReference
    .child('orders')
    .equalTo(myUID, key: 'uid')
    .orderByChild(orderField)
    .limitToFirst(pageSize)
.once()
.then((snapshot) {

  // form current list
  List<dynamic> curretList = [];
  snapshot.value.forEach((orderId, orderData) {
      curretList.add(orderData);
  });

But the following query is returning null result.

I suspect the error is in .equalTo(Provider.of<AppData>(context, listen: false).uid, key: 'uid') . Did I use equalTo correctly here?

The second argument to equalTo that you're trying to use does something completely different than what you need here.

What you're looking for is:

final databaseReference = FirebaseDatabase.instance.reference();
databaseReference
    .child('orders')
    .orderByChild('uid')
    .equalTo(myUID)
    .limitToFirst(pageSize)

So this takes all child nodes of orders , sorts them on the value of their uid property, and then returns the first pageSize nodes that have a uid value equal to myUID .

Since you can only have one orderBy... call per query, this means you'll have to reorder the results by timestamp in your application code. Also see Query based on multiple where clauses in Firebase

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM