在Flutter中按降序订购Firestore文档?

问题描述

我想根据Firestore文档上载的时间戳降序对其进行排序。我的意思是,当我上传新照片时,它应该出现在我制作的图像网格的顶部。在这里,我使用orderBy,并为参数降序传递了true(get images方法中代码的底部)。默认情况下为false,然后图像网格按升序排序。当我上传新图像时,按升序可以正常工作。但是,当我将参数true设置为orderBy内部降序时,它将无法正常工作。它会复制以前上传的图片(请参见下面的图片链接)。但是,当我重新启动应用程序时,它按降序排列。上升是实时更新,但下降不是实时更新。我们只需要更改降序参数的布尔值即可获得升序或降序。如果有人可以帮助我解决此问题,我将不胜感激。这是我的代码,

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);
  }

}

duplicating a previous image

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)