[英]Unable to get document on using where in in flutter firebase (cloud firestore)
I am trying to filter my products based on the price range set by the user.我正在尝试根据用户设置的价格范围过滤我的产品。 For this am using range slider but am not getting any product in this range:- Code for range slider:-
为此,我使用的范围是 slider,但我没有得到该范围内的任何产品:- 范围 slider 的代码:-
SfRangeSlider(
min: 0.0,
max: 20000.0,
showLabels: true,
showTicks: true,
enableTooltip: true,
values: _values,
activeColor: Themes.selectedColor,
inactiveColor: const Color(0xffc0c0c0),
tooltipTextFormatterCallback: (a, s)=>"₹ $s",
onChanged: (SfRangeValues newValues) {
products.removeWhere((element) => element.price!<_values.start&&element.price!>_values.end);
if(products.length<8){
getData();
}
setState(() {
_values = newValues;
});
},
),
and my code to fetch the data:-和我获取数据的代码:-
void getData()async
{
QuerySnapshot snapshot=await productReference.where('category',isEqualTo: widget.category).where("price",whereIn: [_values.start,_values.end]).limit(12).get();
if(snapshot.docs.isNotEmpty){
for (DocumentSnapshot doc in snapshot.docs) {
products.add(ProductModel.fromDocument(doc));
}
lastDoc=snapshot.docs.last;
}
setState(() {
loading=false;
load=false;
});
}
But i am not able to receive any document.但是我无法收到任何文件。 Even though product exists in the price range.
即使产品存在于价格范围内。 For testing i choose 0 and 20,000 as default value to check.
对于测试,我选择 0 和 20,000 作为默认值进行检查。
PS:- Do i need to create any index? PS:- 我需要创建任何索引吗? If so then what values for it?
如果是这样,那么它的价值是什么?
whereIn
checks for exact match with the values you give. whereIn
检查与您提供的值是否完全匹配。
What you want to do is:你想要做的是:
QuerySnapshot snapshot = await productReference
.where('price', isLessThanOrEqualTo: _values.end)
.where('price', isGreaterThanOrEqualTo: _values.start)
.limit(12)
.get();
For your question regarding indexes: If you need to create one, firestore will likely tell you and give you a ling to create it automatically so I would not worry about that.对于您关于索引的问题:如果您需要创建一个索引,firestore 可能会告诉您并给您一个 ling 自动创建它,所以我不会担心。
Another note: I think your method of retrieving data will cause a lot of calls to the database.另一个注意事项:我认为您检索数据的方法会导致对数据库的大量调用。 Maybe a better solution would be to only fetch data when the user stops updating the slider values.
也许更好的解决方案是仅在用户停止更新 slider 值时才获取数据。 Here is how to achieve that:
这是实现该目标的方法:
Listener(
behavior: HitTestBehavior.translucent,
onPointerUp: (_) {
// Listen to pointer up and ONLY retrieve data when this happens
products.removeWhere((element) =>
element.price! < _values.start && element.price! > _values.end);
if (products.length < 8) {
getData();
}
},
child: SfRangeSlider(
min: 0.0,
max: 20000.0,
showLabels: true,
showTicks: true,
enableTooltip: true,
values: _values,
inactiveColor: const Color(0xffc0c0c0),
tooltipTextFormatterCallback: (a, s) => "₹ $s",
onChanged: (SfRangeValues newValues) {
// DON'T fetch the state here, only update the values
setState(() {
_values = newValues;
});
},
),
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.