我一直在尝试对我从firebase数据库获取的数据进行排序,这是我尝试过的代码,但这会引发错误:
(NoSuchMethodError:类“ String”没有实例方法“ orderBy”)。
对数据进行排序的最佳做法是什么?
dateTime: snapshot.data.documents[index].data['dateTime'].orderBy('createdAt', descending: true),
下面,我添加了CrudMethods中的代码。
getData() async {
return await Firestore.instance.collection('PlasmaRequests').orderBy('createdAt', descending: true).snapshots();
}
答案 0 :(得分:1)
您无法在检索后对以下代码进行排序:
snapshot.data.documents[index].data['dateTime']
表示您已检索快照,并且正在获得dateTime
类型的字段String
。
执行查询时,您使用orderBy()
:
void getData() async {
var result = await Firestore.instance
.collection("myColName")
.orderBy('createdAt', descending: true)
.getDocuments();
}
检查文档
https://firebase.google.com/docs/firestore/query-data/order-limit-data