【发布时间】:2020-12-22 12:57:45
【问题描述】:
我想根据上传的时间戳按降序排列 Firestore 文档。我的意思是当我上传一张新照片时,它应该出现在我制作的图像网格的顶部。在这里,我使用 orderBy 并为参数 descending 传递了 true(get images 方法中的代码底部)。默认情况下它是假的,然后图像网格按升序排列。当我上传新图像时,它可以按升序正常工作。但是,当我在 orderBy 中将参数降序设置为 true 时,它不起作用。它制作了先前上传的图像的重复图像(请参见下面的图像链接)。但是当我重新启动应用程序时,它按降序排列。上升是实时更新,但下降不是。我们只需要更改descending 参数的布尔值即可获得升序或降序。如果有人可以帮助我解决这个问题,我将不胜感激。这是我的代码,
class DatabaseService {
final String uid;
DatabaseService({this.uid});
// collection reference
final CollectionReference imageCollection = Firestore.instance.collection('images');
// creating new document for a image user and updating existing image data
Future updateImageData(String location, String url) async{
return await imageCollection.document(uid).setData({
'location': location,
'url': url,
'timestamp': FieldValue.serverTimestamp(),
});
}
// image list from snapshot
List<GridImage> _imageListFromSnapshot(QuerySnapshot snapshot) {
return snapshot.documents.map((doc) {
return GridImage(
url: doc.data['url'] ?? '',
location: doc.data['location'] ?? '',
timestamp: doc.data['timestamp'].toString() ?? ''
);
}).toList();
}
// get image stream
Stream<List<GridImage>> get images {
return imageCollection.orderBy('timestamp', descending: true).snapshots().map(_imageListFromSnapshot);
}
}
【问题讨论】:
-
你必须添加一个索引,尝试使用你的终端你会发现索引的链接
-
@Merym 但是我应该如何给出索引?那么整个gridview必须改变
-
能否显示终端错误
-
你需要确保在firestore中为时间戳创建索引,
-
@Merym 调试中没有错误。如何为时间戳创建索引?你的意思是单独的字段?
标签: firebase flutter google-cloud-firestore flutter-gridview