Firestore query orderBy not working?

喜夏-厌秋 提交于 2020-07-19 07:36:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!