Home > other >  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?

Time:02-13

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

CodePudding user response:

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 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.

  • Related