问题
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) ...
回答1:
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.
回答2:
yep, it's just missing an index. if you're using firestore.indexes.json then add this:
{
"collectionId": "messages",
"fields": [
{ "fieldPath": "toid", "mode": "ASCENDING" },
{ "fieldPath": "_timeStampUTC", "mode": "DESCENDING" }
]
}
回答3:
Use @ServerTimestamp annotation for _timeStampUTC and try to to orderBy query
To make Firestore automatically generate Timestamp values when we upload data to the database, we must use the annotation @ServerTimestamp and set the value to null.
来源:https://stackoverflow.com/questions/51434167/firestore-query-orderby-not-working