【问题标题】:Flutter multiple where case is not working在案例不起作用的情况下颤动多个
【发布时间】:2019-10-06 00:07:45
【问题描述】:

是否有可能基于多个 doc id 获得价值?

CollectionReference col1 =  Firestore.instance
        .collection('service');
     col1.where("title", isEqualTo:"Ac replaciment")
    .where("title",isEqualTo:"Oil Service")
        .getDocuments()

这段代码没有给出任何结果

 CollectionReference col1 =  Firestore.instance
            .collection('service');
         col1.where("title", isEqualTo:"Ac replaciment")

            .getDocuments()

这个代码我得到了结果
但是我的标题是“Ac replaciment”和“Oil Service”但是当我打电话更严厉时它没有给出结果
我需要查询 title =="Oil Service" or title =="Ac replaciment" 如何在 firestore 中使用颤振进行此操作



当我运行此代码时,它会从服务器返回所有数据

CollectionReference col1 = Firestore.instance.collection('service');

    col1.where("title", isEqualTo: "Ac replaciment");

    col1.getDocuments().

但我只需要在title=="Ac replaciment" 为什么会发生此问题时获得结果?正确的代码是什么?

【问题讨论】:

标签: flutter google-cloud-firestore


【解决方案1】:

我能想到两种解决方案。

1.推送两个单独的查询调用并连接结果。

当您向服务器发送两个单独的查询时,这可能会导致更长的数据检索。

CollectionReference col1 = Firestore.instance.collection('service');
final acReplacimentList = await col1.where("title", isEqualTo: "Ac replaciment").getDocuments();
final oilServiceList = await col1.where("title", isEqualTo: "Oil Service").getDocuments();
return acReplacimentList.documents.addAll(oilServiceList.documents);

2。在本地过滤文档。

这可能是一个更快的解决方案,但它会暴露所有其他不必要的文档。

CollectionReference col1 = Firestore.instance.collection('service');
final allList = await col1.getDocuments();
return allList.documents.where((doc) => doc["title"] == "Ac replaciment" || doc["title"] == "Oil Service");

更新

3.使用查询快照

CollectionReference col1 = Firestore.instance.collection('service');
final snapshots = col1.snapshots().map((snapshot) => snapshot.documents.where((doc) => doc["title"] == "Ac replaciment" || doc["title"] == "Oil Service"));
return (await snapshots.first).toList();

【讨论】:

  • 如果我使用的是 firebase 数据库,那么可以选择在一个查询中执行此操作而无需加载所有数据。使用 firestore 是一个错误的选择吗?
  • 还有其他方法吗?你的第二种方法很广泛,第一种方法很慢
  • 你说的是firestore不支持大小写的多重?
  • 我刚刚进行了一些研究。当前过滤器只能执行基本的条件语句。我认为您需要调用和映射查询快照以启用更复杂的过滤器。我会更新我的答案,让我们看看它是否适合您的需要。
  • 现在我正在做你的第一个解决方案,但我不想接受你的答案,因为我正在等待获得更多答案以满足我的需要。对不起
猜你喜欢
  • 1970-01-01
  • 2014-10-12
  • 2013-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-29
  • 2020-11-21
  • 1970-01-01
相关资源
最近更新 更多