【问题标题】:Flutter Firebase: get documents where a particular fiels is not nullFlutter Firebase:获取特定字段不为空的文档
【发布时间】:2021-07-20 16:18:51
【问题描述】:

我正在尝试获取 callerId 字段不包含空值的文档

我试试这个

final collection =
        _firestoreService.instance.collection(TIME_SLOTS_COLLECTION_NAME);

Query query = collection
  .where("listenerId", isEqualTo: _userService.user!.firestoreId!)
  .where("callerId", isNull: false)
  .orderBy("startTime", descending: true);

print("after query");

但它什么也没返回。此语句之后的代码根本不运行。这意味着after query 不会打印在控制台上。不知道有什么问题?

我试过了

final collection =
        _firestoreService.instance.collection(TIME_SLOTS_COLLECTION_NAME);

Query query = collection
  .where("listenerId", isEqualTo: _userService.user!.firestoreId!)
  .where("callerId", isEqualTo: "caller1")
  .orderBy("startTime", descending: true);

print("after query");

它运行但第一个没有。有人知道吗?

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    FlutterFire implementation of isNull看:

    if (isNull != null) {
      assert(
          isNull,
          'isNull can only be set to true. '
          'Use isEqualTo to filter on non-null values.');
      addCondition(field, '==', null);
    }
    

    所以看起来您的条件无效,您应该在程序输出中看到一条错误消息。这也解释了应用停止工作的原因:断言失败,因此您的应用崩溃。

    您可以在此处使用isNotEqualTo(或>>= 等)进行过滤:

    .where("callerId", isNotEqualTo: false)
    

    在这种情况下,我发现真正有用的是将supported data types and their sort order 的表格放在手边。

    【讨论】:

    • 但是callerId 可以取任何值,那么我将如何isNotEqualTo 获取所有具有非空callerId 值的文档?
    • 我在这里很随意地选择了false。听起来callerId 是一个字符串字段,它永远不会有布尔值。你试过查询吗?
    • 嘿@jastor_007 你在这方面有什么进展吗?
    猜你喜欢
    • 2020-09-20
    • 2021-07-04
    • 2020-11-05
    • 2021-02-01
    • 2021-07-10
    • 2022-10-15
    • 2019-02-26
    • 2021-10-26
    • 2021-08-31
    相关资源
    最近更新 更多