[英]How to make a complex query to the firebase in flutter
我的firestore-firebase 数据库如下所示:
products -> collection Name
| uid1 -> Document id
| price : "200"
| category: "a"
| uid2 -> Document id
| price : "500"
| category: "b"
| uid3 -> Document id
| price : "400"
| category: "b"
现在我想获得从200 to 400
的产品,并且category b
,我应该获得产品uid3
现在需要的值取自过滤器屏幕 ui,它有价格范围滑块,然后它甚至有 6-8 个类别,我对尝试的解释是为每个类别创建字段然后查询,但我不这么认为这是一个很好的方法,
因为我必须为类别切换的所有排列组合编写搜索查询,例如如果 categoryA 为 true , categoryB 为 false , categoryC 为 true ,
然后我将不得不为从火力库中获取的所有类别写,
if categoryA is true
...
where('categoryA',isEqualto:"true").get();
if categoryB is true
...
where('categoryB',isEqualto:'false').get();
...
...
...
if categoryA is true categoryB is false categoryC is true
...
where('categoryA',isEqualto:"true")
.where('categoryC', isEqualto:"true).get();
等等等等,这显然是不推荐的。
如何克服这种方法以及如何查询这样的要求
示例场景
我的变量是:
minPrice : String,
maxPrice : String,
categoryA : bool,
categoryB : bool
对于给定的示例属性值是:
minPrice : "200",
maxPrice : "400",
categoryA : false,
categoryB : true,
我想要一个未来的结果,如何在 firebase 中编写这样的查询?
哪些字段应该在 firebase 中被索引以进行有效搜索?
先感谢您 !!
编辑:
这是我为价格尝试的功能,不知道如何为类别做。
Future<List<ProductModel>> getFilteredProducts(String maxPrice, String minPrice) => FirebaseFirestore.instance
.collection("products")
.where("price", isLessThanOrEqualTo: maxPrice)
.where("price", isGreaterThanOrEqualTo: minPrice)
.get()
.then((value) => value.docs.map((e) => ProductModel.fromMap(e.data(), e.id)).toList());
在此处查看有关where
查询的信息
注意:这只是一个示例
await FirebaseFirestore.collection('products'),
.where('price', greaterThanorEqualto: searchText)
.where(categoryB, Equalto: true)
.get()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.