4

I have been struggling with the correct way to perform a query with multiple conditional queries based on some criteria that may or may not be added. I have tried several ways but it's not working.

Future<QuerySnapshot> searchStuff(StuffModel stuff) async {

    CollectionReference col = _firestore.collection(Constants.stuffCollection);

    Query query = col.where('uid', isEqualTo: stuff.uid);

    if (stuff.who != null) {
      query = query.where('who', isEqualTo: stuff.who);
    }
    if (stuff.what != null) {
      query = query.where('what', isEqualTo: stuff.what);
    }
    if (stuff.where != null) {
      query = query.where('where', isEqualTo: stuff.where);
    }
    if (stuff.when != null) {
      query = query.where('when', isEqualTo: Timestamp.fromDate(stuff.when));
    }

    return await query.getDocuments();
  }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Daniel Ortiz
  • 121
  • 1
  • 8
  • 1
    This looks fine to me at first glance. What isn't working about it? – Frank van Puffelen Jun 29 '19 at 20:35
  • it is not returning anything, I have put some breakpoint, the conditions are met, and in database I have documents that met that criteria but nothing. – Daniel Ortiz Jun 29 '19 at 22:28
  • If I do something such as this, it works ```return _firestore .collection(Constants.stuffCollection).where('uid', isEqualTo: stuff.uid).where('what', isEqualTo: stuff.what).getDocuments();``` – Daniel Ortiz Jun 29 '19 at 22:30
  • The code looks fine to me. Are you sure the conditions are the same between the working version and the non-working version? – Frank van Puffelen Jun 30 '19 at 04:29
  • 1
    @FrankvanPuffelen I have fixed the issue, I have added an additional condition for not empty values, when I was testing, in a first run it worked and then in a second test changing parameters, I have notice that one parameter goes with empty value, for instance what == "", so I have added on each condition, for instance stuff.what.isNotEmpty and that conditions fixed the problem. Thanks!! – Daniel Ortiz Jun 30 '19 at 14:52
  • 1
    Good to hear. Can you add an answer that shows the updated code, so that developers can potentially benefit from your work in the future? – Frank van Puffelen Jun 30 '19 at 15:46

1 Answers1

7

After adding additional evaluation, for not empty values, on each condition, the code now looks like this and is working fine:

Future<QuerySnapshot> searchStuff(StuffModel stuff) async {

    CollectionReference col = _firestore.collection(Constants.stuffCollection);

    Query query = col.where('uid', isEqualTo: stuff.uid);

    if (stuff.who != null && stuff.who.isNotEmpty) {
      query = query.where('who', isEqualTo: stuff.who);
    }
    if (stuff.what != null && stuff.what.isNotEmpty) {
      query = query.where('what', isEqualTo: stuff.what);
    }
    if (stuff.where != null && stuff.where.isNotEmpty) {
      query = query.where('where', isEqualTo: stuff.where);
    }
    if (stuff.when != null) {
      query = query.where('when', isEqualTo: Timestamp.fromDate(stuff.when));
    }
    return await query.getDocuments();
}
Daniel Ortiz
  • 121
  • 1
  • 8