Cloud firestore orderby not working on flutter ( error log involved )

Viewed 33

this is my code

  late final Stream<QuerySnapshot> products = productsRef
  .where('submitters', arrayContains: uid)
  .where('postState', isNotEqualTo: 'completed')
  .orderBy('createdAt', descending: true)
  .snapshots();

here's the error log enter image description here

without orderBy it works fine

1 Answers

Compound queries must orderBy on the same field as you are filtering by. In this case it would be postState for you. You are limited to order by that field since you are using it for != comparison.

It is not explicitly mentioned in the documentation, it is hinted in this note on limitations: https://firebase.google.com/docs/firestore/query-data/queries#limitations

Since you are not using a limit() on your query, you can still accomplish the same result by piping your result set and implement an array-sort locally on the createdAt field.

Related