在Flutter中按降序订购Firestore文档? [英] Ordering Firestore documents in descending order in Flutter?

查看:68
本文介绍了在Flutter中按降序订购Firestore文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to order Firestore documents in descending order according to the timestamp they have uploaded. I mean when I upload a new photo it should appear on the top of the image grid that I have made. Here I used orderBy and I passed true for the parameter descending (bottom of the code in the get images method). by default it is false and then the image grid is ordering ascending fine. When I upload a new image it works fine with ascending order. But when I set true to parameter descending inside the orderBy it is not working. It makes a duplicate image of previously uploaded one (see the image link below). But when I restart the app it is ordered descending. ascending is updating realtime but descending is not. We just only have to change the boolean value of descending parameter to get either ascending order or descending order. I would be grateful if someone can help me with this issue. Here is my code,

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

}

复制先前的图像

推荐答案

正如Meryn所说,您应该使用am索引来解决此问题.其实有很多关于此的示例(通过不工作进行排序 firestore命令),建议在所有这些索引中使用索引.您可以按照此处的说明进行操作

As Meryn commented, you should use am index to solve this issue. Actually there are a lot of examples about this (order by not working and firestore order by) and in all of them it is recommended to use the index. You can follow the instructions here to do it

您必须转到Firestore控制台并为要订购的字段编制索引.

You would have to go to your firestore console and index the fields you are ordering by.

转到:索引>>复合材料.

Go to: indexes >> Composite.

下一步:添加索引并等待其激活.

Next: add the index and wait for it to be active.

这篇关于在Flutter中按降序订购Firestore文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆