无法获取有关在 flutter firebase(cloud firestore)中使用 where 的文档
Unable to get document on using where in in flutter firebase (cloud firestore)
我正在尝试根据用户设置的价格范围过滤我的产品。为此,我使用了范围滑块,但没有得到该范围内的任何产品:-
范围滑块代码:-
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;
});
},
),
和我获取数据的代码:-
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;
});
}
但是我无法收到任何文件。即使产品存在于价格范围内。为了测试,我选择 0 和 20,000 作为默认值进行检查。
P.S:- 我需要创建任何索引吗?如果是,那么它的价值是多少?
whereIn
检查与您提供的值是否完全匹配。
您想做的是:
QuerySnapshot snapshot = await productReference
.where('price', isLessThanOrEqualTo: _values.end)
.where('price', isGreaterThanOrEqualTo: _values.start)
.limit(12)
.get();
关于你关于索引的问题:如果你需要创建一个,firestore 可能会告诉你并给你一个 ling 自动创建它所以我不会担心。
另请注意:我认为您检索数据的方法会导致对数据库的大量调用。也许更好的解决方案是仅在用户停止更新滑块值时才获取数据。
以下是实现方法:
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;
});
},
),
)
我正在尝试根据用户设置的价格范围过滤我的产品。为此,我使用了范围滑块,但没有得到该范围内的任何产品:- 范围滑块代码:-
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;
});
},
),
和我获取数据的代码:-
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;
});
}
但是我无法收到任何文件。即使产品存在于价格范围内。为了测试,我选择 0 和 20,000 作为默认值进行检查。
P.S:- 我需要创建任何索引吗?如果是,那么它的价值是多少?
whereIn
检查与您提供的值是否完全匹配。
您想做的是:
QuerySnapshot snapshot = await productReference
.where('price', isLessThanOrEqualTo: _values.end)
.where('price', isGreaterThanOrEqualTo: _values.start)
.limit(12)
.get();
关于你关于索引的问题:如果你需要创建一个,firestore 可能会告诉你并给你一个 ling 自动创建它所以我不会担心。
另请注意:我认为您检索数据的方法会导致对数据库的大量调用。也许更好的解决方案是仅在用户停止更新滑块值时才获取数据。 以下是实现方法:
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;
});
},
),
)