Flutter sort by Timestamp failing

Viewed 22

I have sorted by timestamp in my app before and its worked fine on my messages and such but when I try sorting it for my groupchats it is giving me an error

Structure

Thats my structure and the query im using is

FirebaseFirestore.instance
              .collection('chats')
              .where(
                'users',
                arrayContains: FirebaseAuth.instance.currentUser!.uid,
              )
              .orderBy('lastMessaged', descending: false)
              .get(),

and it is giving me this error

W/Firestore( 4428): (24.2.1) [Firestore]: Listen for Query(target=Query(chats where usersarray_containsOakThEN9ZXcurMnxmzF7H9eOl4J3 order by lastMessaged, __name__);limitType=LIMIT_TO_FIRST) failed: Status{code=FAILED_PRECONDITION, description=The query requires an index. You can create it here: https://console.firebase.google.com/v1/r/project/instagram-clone-5b92a/firestore/indexes?create_composite=ClNwcm9qZWN0cy9pbnN0YWdyYW0tY2xvbmUtNWI5MmEvZGF0YWJhc2VzLyhkZWZhdWx0KS9jb2xsZWN0aW9uR3JvdXBzL2NoYXRzL2luZGV4ZXMvXxABGgkKBXVzZXJzGAEaEAoMbGFzdE1lc3NhZ2VkEAEaDAoIX19uYW1lX18QAQ, cause=null}
1 Answers

The error message gives the solution:

The query requires an index. You can create it here: https://console.firebase.google.com/v1/r/project/instagram-clone-5b92a/firestore/indexes?create_composite=ClNwcm9qZWN0cy9pbnN0YWdyYW0tY2xvbmUtNWI5MmEvZGF0YWJhc2VzLyhkZWZhdWx0KS9jb2xsZWN0aW9uR3JvdXBzL2NoYXRzL2luZGV4ZXMvXxABGgkKBXVzZXJzGAEaEAoMbGFzdE1lc3NhZ2VkEAEaDAoIX19uYW1lX18QAQ

As you can see, the error message contains a URL that you can open to create the missing Firestore index.

More details in the doc, here and here.

Related