Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter + Firebase. Sorting data in chronological order

I add some data to the collection. When i retrive it, it is sorted by that generated id. Is it posible to make firebase to sort it in chronological order or it is required to add one more field with date and sort it once it is retrived in flutter?

like image 909
AAALLL3XXX Avatar asked Oct 05 '19 16:10

AAALLL3XXX


2 Answers

Yes you need a createdAt timestamp field and use it like

Firestore.instance
     .collection("users")
     .orderBy('createdAt', descending: true or false).getDocuments()

And you can store createdAt on flutter side with Timestamp (Its included in cloud_firestore), and you can get current timestamp with Timestamp.now()

like image 168
Ibrahim Karahan Avatar answered Nov 15 '22 09:11

Ibrahim Karahan


There is no built-in metadata on when a document was inserted. If you want to order the documents by insertion order, you will indeed have to add a field with that information yourself and order on that field when retrieving the documents.

like image 36
Frank van Puffelen Avatar answered Nov 15 '22 10:11

Frank van Puffelen