0

My db structure looks like this:

enter image description here

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?

user1187968
  • 7,154
  • 16
  • 81
  • 152

1 Answers1

0

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

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • what if I want to keep my original sort order? – user1187968 Apr 27 '20 at 03:50
  • Also, I have pagination in place, so sorting in application code doesn't work! – user1187968 Apr 27 '20 at 03:52
  • Firebase Database queries can only order/filter on a single property. In many cases it is possible to combine the values you want to filter on into a single (synthetic) property. For an example of this and other approaches, see my answer to the question I linked. – Frank van Puffelen Apr 27 '20 at 03:56