Home > Software engineering >  Flutter Firestore query by date and String simultaneously not returning?
Flutter Firestore query by date and String simultaneously not returning?

Time:06-11

I'm using where query filtering both date and a string value.

var _inShop = FirebaseFirestore.instance.collection('assignment')
  .where('status', isEqualTo: 'Out Of Service')
  .where('arrive', isGreaterThan: Timestamp.fromDate(DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day)))
  .snapshots().map((event) => event.docs.length);

using them separately it works fine.

var _inShop0 = FirebaseFirestore.instance.collection('assignment')
  .where('status', isEqualTo: 'Out Of Service')
  .snapshots().map((event) => event.docs.length);

var _inShop1 = FirebaseFirestore.instance.collection('assignment')
  .where('arrive', isGreaterThan: Timestamp.fromDate(DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day)))
  .snapshots().map((event) => event.docs.length);

but combining them it doesn't display the data.

error handler throws [cloud_firestore/failed-precondition] The query requires an index. You can create it here: and there is a link.

firestore query doesn't allow query for date and string?

CodePudding user response:

The problem is that you have to define an index for this query to be performed in the Firestore.

Log the whole catched exception and you will see a link in the exception, open it in the browser and it will navigate you to a page in Google Cloud Console where you can just click on Create to create the required index, after that you should just wait for the index to be created and then run the query again and you will see that every thing works fine.

To learn more about Firestore indexes, follow this link.

  • Related