0

With latest version of: Flutter + cloud_firestore: ^0.14.0+2

Code:

      FutureBuilder(
      future: _sessionsLogCollection
          .where('companyId', isEqualTo: sPData.companyId)
          .where('locationId', arrayContainsAny: [
            '29L73oSzdQrzLUow3Mg9',
            'bugdWVC6RRtHoemuxNWE',
          ])
          // .where('locationId', isEqualTo: 'bugdWVC6RRtHoemuxNWE')
          .orderBy('openDateTime', descending: true)
          .get(),

I already have indexes created, so that isn't the problem.

When using .where('locationId', isEqualTo: 'bugdWVC6RRtHoemuxNWE') alone, the query returns the correct data.

But with .where('locationId', arrayContainsAny: ['29L73oSzdQrzLUow3Mg9','bugdWVC6RRtHoemuxNWE']) alone, it does not give any error, but simply returns empty data set: sessionsSnapShot.data.documents.length: 0.

** Solved with .whereIn (Probably .whereIn should be included at firebase.flutter.dev/docs/firestore/usage as it is not currently there)

0

1 Answer 1

5

According to your code .where('locationId', isEqualTo: 'bugdWVC6RRtHoemuxNWE'), it seems that locationId is not an array, instead it is a field. If you want to query an array you can only use arrayContains and arrayContainsAny. But since this is a field then you have to use whereIn:

          .where('locationId', whereIn: [
            '29L73oSzdQrzLUow3Mg9',
            'bugdWVC6RRtHoemuxNWE',
          ])

Use the in operator to combine up to 10 equality (==) clauses on the same field with a logical OR. An in query returns documents where the given field matches any of the comparison values

https://firebase.google.com/docs/firestore/query-data/queries#array_membership

https://github.com/FirebaseExtended/flutterfire/blob/master/packages/cloud_firestore/cloud_firestore/lib/src/query.dart#L393

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

2 Comments

That worked perfectly. Problem is I did not find .whereIn at firebase.flutter.dev/docs/firestore/usage yet I can see it now in query.dart. One more thing, when it comes to firebase.google.com/docs/firestore/query-data/… is Flutter/Dart syntax will always be what's under Kotlin+KTX ?
No kotlin+ktx is a different language

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.