I have a Flutter app that successfully shows a ListView of documents within a collection. But now I need certain filters.
I know there's no OR clause in Firestore (although I think there should be), so I've read that I can achieve similar results making two or more queries depending on how many values of the OR conditions I need.
Ok, I understand the logic, but I refuse to believe this is an optimal solution. Despite that I decided to implement that solution into my app, but still can't achieve success.
Imagine a scenario: I have a collection of "employee" documents. A lot. And there are 4 types of Employee:
[Production, Administrative, Security, External]
My app pretends to allow filters depending on the type of employee. So if at certain moment I want only Production and Administrative types I would have to:
Query q1 = collectionReference.where('type', isEqualTo: 'Production').orderBy('name');
Query q2 = collectionReference.where('type', isEqualTo: 'Administrative').orderBy('name');
QuerySnapshot querySnapshot1 = await q1.getDocuments();
QuerySnapshot querySnapshot2 = await q2.getDocuments();
...but then How I can merge the two queries into one?.
And, since I have thousands of employees in the Database I paginate them using the limit(20)
and startAfter(Document)
clauses. The ideal is that the snapshot gives me the right number of documents, with the right conditionals and with the right order (name).
Addendum: In Java you can try this below, but what about Dart?
Task task1 = employees.whereLessThan("type", "Production") .orderBy("name") .get();
Task task2 = employees.whereGreaterThan("type", "Production") .orderBy("name") .get();
Task<List<QuerySnapshot>> allTasks = Tasks.whenAllSuccess(task1, task2);
allTasks.addOnSuccessListener(new OnSuccessListener<List<QuerySnapshot>>() {
@Override
public void onSuccess(List<QuerySnapshot> querySnapshots) {
...
}
}