【问题标题】:Flutter - How to check multiple conditions while fetching data from firebase realtime database? [duplicate]Flutter - 如何在从 firebase 实时数据库中获取数据时检查多个条件? [复制]
【发布时间】:2020-07-26 10:19:57
【问题描述】:

我正在尝试从 firebase 获取大量数据。当我通常使用 single where 子句加载所有数据时,加载大约需要 1 分钟。我想添加多个 where 子句,以便我可以在获取时进行检查,以便通过不获取不需要的数据来优化时间。如何在从 firebase 实时数据库中获取数据时检查多个条件

这是我的代码:

 var query = FirebaseDatabase.instance.reference().child(str_gender)
        .child('Facial Parts/Nose').orderByChild("noselength").equalTo(str_length);



这里我只能通过鼻子长度来检查,但我想通过鼻子长度、鼻子类型、鼻子宽度来过滤掉它

我们可以用其他语言说

noselength=value and nosewidth=value and nosewidth=value

如果条件满足,那么只有我需要获取数据

那么如何在我现有的代码中添加多个 where 子句??

【问题讨论】:

  • Firebase 数据库查询只能对单个属性进行排序/过滤。在许多情况下,可以将要过滤的值组合成一个(合成)属性。有关此方法和其他方法的示例,请在此处查看我的答案:stackoverflow.com/questions/26700924/…

标签: firebase flutter dart firebase-realtime-database


【解决方案1】:

您不能在实时数据库中添加多个条件。一种解决方案是创建一个包含所有 3 个字段的属性:

length_width_type : 10_10_normal

然后做:

 var query = FirebaseDatabase.instance.reference().child(str_gender)
        .child('Facial Parts/Nose').orderByChild("length_width_type").equalTo(value);

其他解决方案是使用firestore,因为它支持多个where 子句:

void getData() async {
  var result = await Firestore.instance
      .collection("str_gender")
      .where("noseLength", isEqualTo: 10)
      .where("noseWidth", isEqualTo: 10)
      .getDocuments();
}

【讨论】:

    猜你喜欢
    • 2019-09-29
    • 2021-03-04
    • 2021-04-08
    • 2021-02-06
    • 2020-07-12
    • 2023-04-03
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多