Home > Software design >  Firestore Query with .where conditions
Firestore Query with .where conditions

Time:10-07

I am using the following query:

Query postsQuery = FirebaseFirestore.instance.collection('posts')
  .where('postType', isNotEqualTo: 'Discussion') //post types - Simple, featured, discussion
  .orderBy('createdAt', descending: true)
  .where('group', isEqualTo: groupName) //india, usa, pakistan
  .limit(perPage);

The query runs if i remove this :

.where('postType', isNotEqualTo: 'Discussion')

Anyone knows what i am doing wring here?

CodePudding user response:

It seems that you haven't declare the Firestore index corresponding to your query.

As explained in the doc:

Cloud Firestore ensures query performance by requiring an index for every query. The indexes required for the most basic queries are automatically created for you. As you use and test your app, Cloud Firestore generates error messages that help you create additional indexes your app requires.

...

If you attempt a compound query with a range clause that doesn't map to an existing index, you receive an error. The error message includes a direct link to create the missing index in the Firebase console.

So, add a catch() block to the code used to execute the query and you will get the link in the error message. Note: This link is to be opened (once) via a web browser, not via the Flutter app.

  • Related