[英]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.