Mousa Alribi
Mousa Alribi

Reputation: 300

How to make query spot only the some value in a Map inside an Array in flutter (dart)?

I am working on this project where I have to put data in maps inside in array for example: [{Rate: 1.3, Title: "Test",...}] What I would like to do to find documents which has maps inside their array with rating 1.3 and above without fetching all arrays and filtering it internally

I have tried to this

      Firestore.instance
          .collection("profiles")
          .where("questions", arrayContains: {"Rate": 1.3})
          .getDocuments(source: Source.serverAndCache)

this code actually requires me to put all map data so it could return true otherwise it will return false. What I want is just to filter data based on some of the map data

Upvotes: 1

Views: 1558

Answers (2)

Joost
Joost

Reputation: 13

Thanks to SO's 50 point limitation, I can't add a comment to the last solution from 2019.

This does not work today:

.where("questions", arrayContains: {Rate: 1.3, Title: "Test"})

This will result in an error where Rate is undefined

My Firestore collection 'contracts' looks like this: Firestore Screenshot

Visual Code Code Screenshot

I have tried many other solutions in other threads (whereIn, arrayContainsAny), but I cant figure out how to query an array that holds a series of maps (0, 1, 2 etc).

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 599946

The arrayContains operator checks for the complete element's presence in the array. So if your array contains an element {Rate: 1.3, Title: "Test"}, you need to:

.where("questions", arrayContains: {Rate: 1.3, Title: "Test"})

If you want to be able to test only for Rate, create an additional array Rates where you keep just those values. Then you can query with:

.where("rates", arrayContains: 1.3)

Upvotes: 3

Related Questions