0

The new version cloud_firestore: ^4.5.1 allows filter by "or" and "and" by this way:

Future<List> getListModelos() async {
  List objetoList1 = [];
  Query<Map<String, dynamic>> collectionReferenceObjetoList1 =
      db.collection('/modelos').where(
            Filter.and(
              Filter("name", isEqualTo: "John"),
              Filter("id", isEqualTo: "01"),
            ),
          );
  QuerySnapshot queryObjetoList1 = await collectionReferenceObjetoList1.get();
  queryObjetoList1.docs.forEach((documento) {
    objetoList1.add(documento.data());
  });
  return objetoList1;
}

I found this helpfull info here: How to add an OR condition to flutter Firebase queries

But, when trying "isEqualTo" + "isNotEqualTo" the query not returns nothing:

db.collection('/modelos').where(
            Filter.and(
              Filter("name", isEqualTo: "John"),
              Filter("id", isNotEqualTo: "null"),
            ),
          );

Is there any way or suggestion to query from Flutter to Firebase that allow "select where isEqual and isNotEqual"?

xpe
  • 19
  • 5

1 Answers1

0

The two most likely problem here is that this query requires a composite index, and those are not automatically created. If I run the query without creating an index, it logs this error:

cloud_firestore/failed-precondition] The query requires an index. You can create it here: https://console.firebase.google.com/v1/r/project/_/firestore/indexes?create_composite=Clxwcm9qZWN0cy9wcm9qZWN0LTgwODAwNTkzMjUyODIwOTgxODQvZGF0YWJhc2VzLyhkZWZhdWx0KS9jb2xsZWN0aW9uR3JvdXBzLzc2MDE1MDg5L2luZGV4ZXMvXxABGggKBG5hbWUQARoGCgJpZBABGgwKCF9fbmFtZV9fEAE

That link in there takes you directly to the Firestore console page to create the necessary index, with all details already filled out. All I had to do is click the button, and wait for the index to be created. Once the index has been created, my query returns the documents where name is "John" and with an id field whose literal string value is not equal to "null".

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Great! I followed the steps given in your suggestion. It works!!! I apreciate your answer very much. Regards. – xpe Apr 14 '23 at 21:01