1

I'v ben struggled with this simple/multiple query from firebase & dart.

I need filter this entities 'meals' by creatorId and by %text%

.orderByChild('title')
.orderByChild('creatorId')

I apreciate any help!

await DBRef.reference()
    .child('meals')
    .orderByChild('title')
    .startAt(text)
    .orderByChild('creatorId')
    .equalTo(userId)
    .once()
    .then((DataSnapshot snapshotResult) {
  snapshot = snapshotResult;

  if (snapshot == null || snapshot.value==null ) return;
  map = snapshot.value;
  print(map.values.toList().toString());
  // print( 'map ' + map.toString());
});

I'm getting this error when put this two orderByChild

'!_parameters.containsKey('orderBy')': is not true.

So, how I suppose to filter with two params?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
exequielc
  • 681
  • 1
  • 11
  • 32
  • 1
    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 here: https://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Nov 16 '19 at 21:24
  • @exequielc if the answer helped you please mark it as correct and upvote it so others know that it is useful, thank you! – Peter Haddad Nov 17 '19 at 18:43

1 Answers1

1

In firebase real time database you cannot do this filter. You can only use one orderByChild() and not multiple. To fix this you can change your database to the following for example:

random id
     meals
       random id
             title : "chicken"
             creatorId : "1234"
             title_creator : "chicken_1234"

await DBRef.reference()
    .child('meals')
    .orderByChild('title_creator')
    .equalTo("chicken_1234")
    .once()
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134