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)

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
YaGeorge
  • 47
  • 10

1 Answers1

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

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • That worked perfectly. Problem is I did not find .whereIn at **https://firebase.flutter.dev/docs/firestore/usage/** yet I can see it now in query.dart. One more thing, when it comes to **https://firebase.google.com/docs/firestore/query-data/queries#array_membership** is Flutter/Dart syntax will always be what's under Kotlin+KTX ? – YaGeorge Sep 05 '20 at 06:01
  • No kotlin+ktx is a different language – Peter Haddad Sep 05 '20 at 06:07