Home > Back-end >  Getting data from firebase with where using flutter
Getting data from firebase with where using flutter

Time:02-18

I will pull data from firebase according to the search word, but I had a problem using "where", how can I solve it?

enter image description here

i want to filter by value under name

 FirebaseFirestore.instance
                .collection('tedaviler')
                .where("value.name", arrayContains: name)
                .snapshots()

this is not working

CodePudding user response:

name is not an array in your DB. It is a string. You should try

 FirebaseFirestore.instance
                .collection('tedaviler')
                .where("value.name", isEqualTo: name)
                .snapshots()

CodePudding user response:

Firestore's array-contains operation tests whether the value that you pass exists as an item in the array you identify. The value you pass must match the complete item in the array, there is no option to check just some of the properties.


In your code you have a condition:

.where("value.name", arrayContains: name)

This means you're looking for a field value at the root of the document, and then a subfield name under that. But your value field is an array, and it doesn't have a subfield name. Each item in the array may have a name subfield, but that is not the same.

So the condition is never met, and your query returns no documents.


If you want to test the items in the value array, you will have to know the entire item. If you have that, the condition becomes:

.where("value", arrayContains: { 
  "id": "IzotonikId123", 
  "name": "IV Izotonik %1", 
  "price": 8.88 
})

If you want to be able to search on just the names, you'll need to add another array field to the document, where you keep just the (unique) value names:

valueNames: ["IV Izotonik %1", "IV Izotonik %2", "IV Izotonik %3"]

Then you can query for the existing of a specific name with:

.where("valueNames", arrayContains: name)
  • Related