【问题标题】:Flutter Firestore, sorting data by dateFlutter Firestore,按日期排序数据
【发布时间】:2021-01-12 17:33:26
【问题描述】:

这是我的搜索代码。我正在使用此代码从 Firestore 获取卡。我正在尝试设置按日期排序我的卡片。

我该怎么做。感谢您的帮助。

class SearchService {
      List<Future<QuerySnapshot>> searchByName() {
        return [
          Firestore.instance
              .collection('dis')
              .where('no')
              .getDocuments(),
    
    
        ];
      }
    }

【问题讨论】:

  • 您查询的“否”是什么?顺便说一句,在 Flutter Firestore 中查看这个查询:Flutter Firestore stackoverflow.com/questions/48937864/…
  • 它的字符串编号如 123456
  • 那么,你是如何根据日期排序的??
  • 我可以将“否”更改为时间戳,但我仍然不知道如何按日期排序

标签: android firebase flutter dart google-cloud-firestore


【解决方案1】:

“where”子句不会为您对查询结果进行排序。为此,您需要使用orderBy。您还需要知道用于订购的日期字段的名称,如documentation 所示。

          Firestore.instance
              .collection('dis')
              .orderBy('date')
              .getDocuments(),

如果您没有为每个文档存储日期的字段,您将无法对结果进行排序。

【讨论】:

  • 另外,如果遇到索引错误,您需要在firebase中自己创建一个索引。
  • 可以通过添加orderBy('date', descending: true)来改变排序顺序,
猜你喜欢
  • 1970-01-01
  • 2021-01-08
  • 2020-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-10
  • 1970-01-01
  • 2021-11-02
相关资源
最近更新 更多