【问题标题】:Unable to get document on using where in in flutter firebase (cloud firestore)无法获取有关在颤动 Firebase 中使用 where in 的文档(云 Firestore)
【发布时间】:2021-11-23 06:49:30
【问题描述】:

我正在尝试根据用户设置的价格范围过滤我的产品。为此,我正在使用范围滑块,但在此范围内没有得到任何产品:- 范围滑块的代码:-

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:- 我需要创建任何索引吗?如果是这样,那么它的价值是什么?

【问题讨论】:

    标签: flutter dart google-cloud-firestore rangeslider


    【解决方案1】:

    whereIn 检查与您提供的值是否完全匹配。

    你想做的是:

    QuerySnapshot snapshot = await productReference
        .where('price', isLessThanOrEqualTo: _values.end)
        .where('price', isGreaterThanOrEqualTo: _values.start)
        .limit(12)
        .get();
    

    对于您关于索引的问题:如果您需要创建一个,firestore 可能会告诉您并给您一个自动创建它的指令,所以我不会担心。

    另一个注意事项:我认为您检索数据的方法会导致对数据库的大量调用。也许更好的解决方案是仅在用户停止更新滑块值时获取数据。 以下是实现这一目标的方法:

    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;
          });
        },
      ),
    )
    

    【讨论】:

    • 谢谢你成功了。但是你能告诉我如何在滑动滑块时获取数据,就像当前函数一样,获取数据函数运行 n 否。次数
    • 我不明白你的问题。你想让你的函数运行得更少吗?
    • 是的,我希望它仅在用户停止更改滑块时运行
    • 我更新了我的评论。我用 products.length
    • 感谢您,但我找到了更好的解决方案。当用户停止移动滑块时获取数据。
    猜你喜欢
    • 2022-01-24
    • 2021-08-06
    • 1970-01-01
    • 2021-12-22
    • 2021-07-18
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    • 2020-12-16
    相关资源
    最近更新 更多