【问题标题】:Ordering Firestore documents in descending order in Flutter?在 Flutter 中按降序排列 Firestore 文档?
【发布时间】: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);
  }

}

duplicating a previous image

【问题讨论】:

  • 你必须添加一个索引,尝试使用你的终端你会发现索引的链接
  • @Merym 但是我应该如何给出索引?那么整个gridview必须改变
  • 能否显示终端错误
  • 你需要确保在firestore中为时间戳创建索引,
  • @Merym 调试中没有错误。如何为时间戳创建索引?你的意思是单独的字段?

标签: firebase flutter google-cloud-firestore flutter-gridview


【解决方案1】:

正如 Meryn 所说,您应该使用 am index 来解决这个问题。实际上有很多关于这个的例子(order by not workingfirestore order by)并且在所有这些例子中都推荐使用索引。你可以按照here的指示去做

您必须转到您的 Firestore 控制台并索引您订购的字段。

转到:索引 >> 复合。

下一步:添加索引并等待它被激活。

【讨论】:

  • 感谢您的回答。后来我发现问题出在分页上。无论如何,我可以从您的回答中知道 Firebase 索引很重要。我可以用它来解决未来的问题。再次感谢您。
【解决方案2】:

修改 GridImage 以使用 equatable 这样

import 'package:equatable/equatable.dart';

class GridImage extends Equatable {
  final String location;
  final String url;
  final String timestamp;

  GridImage({this.location,this.url,String timestamp}):timestamp = timestamp ?? Timestamp.now().toString(); // incase timestamp is null
  

  @override
  List<Object> get props => [location,url];
}

修改你的 _imageListFromSnapshot 函数

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() // remove null check we are already handling it inside the model and use null safe call ( ?. )
  );
}).toList().toSet().toList(); // convert the list to set to remove duplicates

}

【讨论】:

  • 确保使用?。在时间戳上调用 toString 方法时
  • 感谢您的回答。后来发现问题出在分页上。
猜你喜欢
  • 2018-05-06
  • 1970-01-01
  • 2021-09-10
  • 2021-02-20
  • 2022-06-10
  • 1970-01-01
  • 1970-01-01
  • 2019-11-03
  • 2021-07-11
相关资源
最近更新 更多