0

I can't find documentation or example how to use multiple where clauses in Flutter Stream Builder returned by Firestore Collection.

The bellow query works

      Firestore
          .instance
          .collection (collectionName)
          .where      ("index",   arrayContains:  search)
          .orderBy    ('date',    descending:     true  )
          .snapshots()

but return zero if I add an additional where clause

      Firestore
          .instance
          .collection (collectionName)
          .where      ("index",   arrayContains:  search)
          .where      ('allowed_roles', arrayContains: GlobalVars.userRole.toString() )
          .orderBy    ('date',    descending:     true  )
          .snapshots() 

The compound query examples in Firebase documentation look pretty much as above but there is missing Flutter or Dart examples.

EDITED : I have also tried the query build pattern as workaround but it doesn't work neither. Flutter really doesn't want the arrayContains clause more than once.

  CollectionReference collectionRef = Firestore.instance.collection (collectionName);
  Query p =  collectionRef.where      ('allowed_roles', arrayContains: "3");
  Query q =  p.where      ('index', arrayContains: "Dan");

  Stream s = q.snapshots(); 

What I am missing here is what solution should programmer use for this problem a logical AND is a pretty basic equation and it is unusual to stuck on this feature.

5
  • Your code doesn't seem to have any error checking. Are you getting an error? What does it say? What exactly isn't working the way you expect? Commented May 20, 2020 at 15:44
  • There is no error, it behaves as an empty query as soon as I use two where clauses and it starts working as soon as you provide one or other where clause. So it is clear that the queries are not exclusive. This post explains that flutter follows "build query pattern" stackoverflow.com/questions/50316462/… BUT the build query pattern breaks the Stream Builder so I am still checking a legit source how this is supposed to be written Commented May 20, 2020 at 16:00
  • Are you sure there's no error? The documentation suggests that you can't have more than one array-contains filter per query. Commented May 20, 2020 at 16:03
  • No I have nothing in the logs and the app is behaving like if the query is returning nothing. It doesn't stuck it execute it self without any error. So your mention about one array filter per query make sense. Now the question is how perform a logical AND in this case Commented May 20, 2020 at 16:09
  • 2
    I'll point out that you have no code doing actual error checking. I wouldn't expect everything to just show up in the log. And that the documentation suggests you can't have two array-contains filters. firebase.google.com/docs/firestore/query-data/… Commented May 20, 2020 at 16:11

2 Answers 2

3

You can't have two arrayContains filters in a query:

      .where      ("index",   arrayContains:  search)
      .where      ('allowed_roles', arrayContains: GlobalVars.userRole.toString() )

According to the documentation, only one arrayContains is supported at a time. This is a limitation of Firestore and can't be changed. The query is certainly generating an error - you should add code to check for errors instead of blindly assuming that it succeeds.

Sign up to request clarification or add additional context in comments.

1 Comment

That's a completely different query and doesn't involve arrays at all. It has one equality filter and one range filter, which is valid.
0

As brought up in the comments Firebase doesn't take more than one arrayContains clause .

The only solution for my case is to hooking a conditional statement for allow_roles array in my activities :

if( document['allowed_roles'].contains( GlobalVars.userRole ))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.