Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore query orderBy not working?

FutureBuilder with a Firestore query on a field of Type timestamp comes back with no data in snapshot. However, the same query without the orderBy works just fine.
What am I missing ? Thanks for the help.

// Working code
future: Firestore.instance.collection('messages').where('toid',isEqualTo: _emailID).getDocuments(),
builder: (context, snapshot) ...

// Not Working - returns to if(!snapshot.hasData)
future: Firestore.instance.collection('messages').where('toid',isEqualTo: _emailID).orderBy('_timeStampUTC', descending: true).getDocuments(),
builder: (context, snapshot) ...

enter image description here

like image 760
AhabLives Avatar asked Jul 20 '18 02:07

AhabLives


People also ask

How do I sort in firestore?

You can specify the sort order for your data using orderBy() , and you can limit the number of documents retrieved using limit() . Note: An orderBy() clause also filters for existence of the given field. The result set will not include documents that do not contain the given field.

How do I save a timestamp on firestore?

If you want to use server generated value which would be better so you don't depend on device time, use this: Map<String, dynamic> data = { 'name': userName, 'userUID': userUIDglobal, 'time': FieldValue. serverTimestamp(), }; Firestore.

How much data can firebase handle?

256 MB from the REST API; 16 MB from the SDKs. The total data in each write operation should be less than 256 MB. Multi-path updates are subject to the same size limitation.


1 Answers

I think you are missing a ' here '_timeStampUTC, so it should be:

 orderBy('_timeStampUTC', descending: true)

EDIT:

Also, you need to be sure to create an index for toid and other for _timeStampUTC, this is done when you try to order by a property that is not in you the where of the query.

like image 186
Rodrigo Mata Avatar answered Nov 10 '22 23:11

Rodrigo Mata