'how we can run multiple queries on Firebase/firestore to get filtered data in flutter?

how we can run multiple queries on Firebase/ firestore to get filtered data in flutter or any other possible solution? there is a picture storing dummy data in goal and title and wanna users according to the goals and titles filters. filter UI

firebsae data



Solution 1:[1]

Where() is a function provided by Firebase to query data

FirebaseFirestore.instance.collection('yourCollectionName').where('field1', isEqual:'value1').where('field2', isEqual: 'value2').get();

you can read more about it in the querying section at https://firebase.flutter.dev/docs/firestore/usage/

Solution 2:[2]

By looking at your Firestore database it is clear that you want to filter the documents based on two array fields goals and titles.

Firestore supports the arrayContains filter in the where clause by which you can filter data from an array inside Firestore. So you may think of doing something like this -

var filter1 = 'Hire Employees';
   var filter2 = 'CEO';
   FirebaseFirestore.instance
       .collection('collectionId')
       .where('goals', arrayContains: filter1)
       .where('titles', arrayContains: filter2)
       .get()
       .then((QuerySnapshot docs) => {
             for (var element in docs.docs) {print(element.data())}
           });

But the above will throw an error saying that You cannot use 'array-contains' filters more than once. This is because Firestore has a limitation that you can use at most one array-contains clause per query which is documented here. So it is not possible to do something like the above.

I am not very sure of your use case, but as a workaround, you can restructure the database structure by making any one of the array field to a map, something like the following -

enter image description here

By making the structure like the above you can use multiple where clauses with one arrayContains filter and one isEqualTo filter which will look something like this -

   var filter1 = 'Hire Employees';
   var filter2 = 'CEO';
   FirebaseFirestore.instance
       .collection('collectionId')
       .where('goals.$filter1', isEqualTo: true)
       .where('titles', arrayContains: filter2)
       .get()
       .then((QuerySnapshot docs) => {
             for (var element in docs.docs) {print(element.data())}
           });

As the idea of allowing multiple arrayContains filters is currently not supported, but could be valid, I would recommend you file a feature request.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dania Rachid
Solution 2