Reputation: 91
I have a list of Strings that is filled dynamically(at mas 5 items). Now i want to filter my documents from firsestore based on those items, so i wrote this:
List<String> lst= new List();
lst.add('A');
lst.add('B');
lst.add('C');
Query collectionRef = Firestore.instance
.collection("files")
.document(Userpref.language)
.collection("files")
.where("describeFear", arrayContainsAny: [
lst
]);
Inside the [] if i put the list name it will not work. Also if i put all the items in the list in a variable and pass it to the [] it will not work .
The only time it works is when I put the values like this 'A','B','C'. This works, but i need to pass the array couse its dynamic . Can anyone help ?
Upvotes: 0
Views: 4657
Reputation: 730
You need to be using whereIn instead of arrayContainsAny for this use case
List<String> lst= new List();
lst.add('A');
lst.add('B');
lst.add('C');
Query collectionRef = Firestore.instance
.collection("files")
.document(Userpref.language)
.collection("files")
.where("describeFear", whereIn: [
lst
]);
Upvotes: 5
Reputation: 57
With latest version of: Flutter + cloud_firestore: ^0.14.0+2
I'm facing a similar issue, the query is returning empty data:
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.
Edited: when locationId is an array itself, then arrayContainsAny: will work. Yet, if locationId is a String field, then whereIn: should be used instead.
Upvotes: 2
Reputation: 598728
You're putting your List
object into an array, so you end up looking for documents that have an array where one single item is ["A", "B", "C"]
.
What you want is:
List<String> lst= new List();
lst.add('A');
lst.add('B');
lst.add('C');
Query collectionRef = Firestore.instance
.collection("files")
.document(Userpref.language)
.collection("files")
.where("describeFear", arrayContainsAny: lst);
Upvotes: 1