0

Hello I have a question about google firestore, I have read that it is impossible to create and clause queries but I need a query like this but if I made it like this it fill my memory garbage. Do you have any solution to make a similar query to keep fast and optimized execution ?

getPlaceByZipCode(int selectedZipCode) {
    try {
      _resultFoundPlacesStream = FirebaseFirestore.instance
          .collection(PLACES_COLLECTION)
          .where('cityZipCode', isEqualTo: selectedZipCode)
          .snapshots()
          .listen((event) {
        event.docs.map((e) {
          if (e.get('ownerId') == null)
            _resultFoundPlaces
                .add(PlaceModel.fromDocumentSnapshot(documentSnapshot: e));
        }).toList();
        print(_resultFoundPlaces.length.toString() +
            ' Places found located in ' +
            selectedZipCode.toString());
      });
    } catch (e) {
      Get.snackbar('app_error'.tr, 'app_error_description'.tr);
    }
  }
John63s
  • 11
  • 4

2 Answers2

0

If you want to filter on specific values for two fields, you can just call where twice:

_resultFoundPlacesStream = FirebaseFirestore.instance
    .collection(PLACES_COLLECTION)
    .where('cityZipCode', isEqualTo: selectedZipCode)
    .where('ownerId', isEqualTo: "ownerIdYouWantToFilterOn")
    .snapshots()
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I made this before but it does not work. It only take the first where clause not the second one. My query would be like this : ``` _resultFoundPlacesStream = FirebaseFirestore.instance .collection(PLACES_COLLECTION) .where('cityZipCode', isEqualTo: selectedZipCode) .where('ownerId', isEqualTo: null) .snapshots() ``` because I want to check if the ```ownerId``` is null. But it doesn't work, it shows me even if the ```ownerId``` is not null – John63s Oct 09 '21 at 15:02
  • Perhaps missing indexes ? – John63s Oct 09 '21 at 15:08
  • If a query requires an index, calling `snapshots()` will return/log an error with a direct link to create the necessary index, so you'll want to look fo that. But does the `ownerI` field actually exist for the docs that you try to capture with `.where('ownerId', isEqualTo: null)`. That seems doubtful, and Firestore only returns fields that actually exist (as others don't exist in the index on that field). See https://stackoverflow.com/questions/46806860/how-to-query-cloud-firestore-for-non-existing-keys-of-documents – Frank van Puffelen Oct 09 '21 at 16:15
  • By default I create a ```ownerId``` with null value, and it exists in my document. – John63s Oct 10 '21 at 15:04
0

I found the solution, I didn't see that there was a special property: isNull. I use: .where('ownerId', isNull: true) and now it works has expected. Thank you for your answers.

John63s
  • 11
  • 4