【问题标题】:Making queries offline on local snapshot copy of Firestore Snapshot Query在 Firestore Snapshot Query 的本地快照副本上进行离线查询
【发布时间】:2021-04-09 03:08:21
【问题描述】:

我在 Firestore 的文档中有以下字段

Category: Online, OnSite, 
Status: true, false,
postTime: mm.dd.yyyy.hh.mm.ss

我正在使用以下方法从 Firebase 中获取快照,并在状态和 postTime 上进行过滤。

 Future getData() async {
    QuerySnapshot qn = await FirebaseFirestore.instance
        .collection("items")
        .where("status", isEqualTo: true)
        .orderBy("postTime", descending: true)
        .get();

    return qn.docs;
  }

现在我有 qn 和 Firestore 数据的副本,我可以在 qn 上运行更具体的查询OFFLINE,在那里我可以使用 Category 进一步过滤它吗?

目的是:如果应用用户想要进一步过滤/排序某些数据,那么我不想为每个查询使用 Firestore 中的数据,因为已经获取了主要数据。

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    Dart 有一个方便的方法,称为 where 用于可迭代对象,您可以利用它以各种方式进行过滤。

    https://api.flutter.dev/flutter/dart-core/Iterable/where.html

    例子:

    final filteredDocs = qn.docs.where((doc) => doc.data()['status'] == 'pending').toList();
    

    【讨论】:

    • 在我的逻辑稍作调整后工作。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2011-12-16
    • 2021-06-17
    • 2020-06-06
    • 1970-01-01
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多