Flutter firestore compound querying not working

274

If you're missing a required index, the SDK will log an error saying so. This error includes a direct link to the Firebase console to create the required index, with all fields already populated. I recommend looking for such a message if you suspect the problem may be due to a missing index.

Share:
274
ismailfarisi
Author by

ismailfarisi

Updated on January 03, 2023

Comments

  • ismailfarisi
    ismailfarisi over 1 year

    i have a compound firestore query. which dosent give any data but when called single it works this is the compound query

     final companyQuery = await _userCollection
              .collection("invoices")
              .where("company.id", isEqualTo: companyId)
              .where("date",
                  isGreaterThanOrEqualTo: Timestamp.fromDate(dateTimeRange.start),
                  isLessThanOrEqualTo: Timestamp.fromDate(dateTimeRange.end))
              .get();
    

    but when i call like this it works

     final companyQuery = await _userCollection
              .collection("invoices")
       
              .where("date",
                  isGreaterThanOrEqualTo: Timestamp.fromDate(dateTimeRange.start),
                  isLessThanOrEqualTo: Timestamp.fromDate(dateTimeRange.end))
              .get();
    

    also works like this too

    inal companyQuery = await _userCollection
              .collection("invoices")
              .where("company.id", isEqualTo: companyId)
              .get();
    

    my document screenshot firestore document

    company objected expanded enter image description here

    this is the print statement

    companyId: lJHLM9sfcwXyPXo18iCX , dateTimeRange: 2022-01-30 00:00:00.000Z - 2022-03-02 21:25:13.563
    [cloud_firestore/failed-precondition] The query requires an index. You can create it here:
    https://console.firebase.google.com/v1/r/project/habllen/firestore/indexes?create_composite=Ckhwcm9qZWN0cy9oYWJsbGVuL2RhdGFiYXNlcy8oZGVmYXVsdCkvY29sbGVjdGlvbkdyb3Vwcy9pbnZvaWNlcy9pbmRleGVzL18QARoOCgpjb21wYW55LmlkEAEaCAoEZGF0ZRABGgwKCF9fbmFtZV9fEAE
    error: FirestoreFailure.unexpected()
    

    my index is

    enter image description here

    it dosent work when it queried together. please help me solve this issue. I have also tried adding composite index

    • Frank van Puffelen
      Frank van Puffelen about 2 years
      1) Can you edit your question to show a screenshot of a document in the Firestore console that you expect to be returned by this query? --- 2) Can you print(companyId) and add the updated code and its output to your question?
    • ismailfarisi
      ismailfarisi about 2 years
      I have edited the question. the problem is not with that query.problem only exist when two queries are compounded
    • Frank van Puffelen
      Frank van Puffelen about 2 years
      You have done neither of the things I asked in my first comment, which makes it hard to help.
    • ismailfarisi
      ismailfarisi about 2 years
      sorry for late reply, i have edited as you asked.
    • ismailfarisi
      ismailfarisi about 2 years
      the documentation says ''"You can chain multiple equality operators (== or array-contains) methods to create more specific queries (logical AND). However, you must create a composite index to combine equality operators with the inequality operators, <, <=, >, and !="'' i tried making composite query by ascending the field company.id and descending the field date
    • Frank van Puffelen
      Frank van Puffelen about 2 years
      If you're missing a required index, the SDK will log an error saying so. This error includes a direct link to the Firebase console to create the required index, with all fields already populated. So I recommend looking for such a message if you suspect the problem may be due to a missing index.
    • ismailfarisi
      ismailfarisi about 2 years
      thanks the problem is solved. before i created index manually but when i followed that link it created index and it solved my problem
  • ismailfarisi
    ismailfarisi about 2 years
    the documentation says it cant do range queries on different fields. but I have one equality and range filter which is valid in the documentation