简体   繁体   English

无法获取有关在 flutter firebase(云 Firestore)中使用何处的文档

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

相关问题 如何从 flutter 中 user.uid 等于文档 ID 的 Cloud firestore 获取数据? - how to get data from cloud firestore where user.uid equal to document id in flutter? Flutter Firebase Cloud Firestore 通过 ID 获取文档列表 stream - Flutter Firebase Cloud Firestore get stream of list of documents by their ids Firestore - 获取路径未知的 ID 文档 - Firebase Cloud Functions - Firestore - Get document with ID whose path is unknown - Firebase Cloud Functions Firestore接管一分钟搞定里面的文档 Firebase Cloud Function - Firestore take over a minute to get a document inside Firebase Cloud Function 无法从 flutter Cloud firestore 获取一份文件 - Can't get one document from flutter cloud firestore FLUTTER:如何在cloud firestore 中使用where 子句删除多个文档? - FLUTTER: How to delete multiple documents using where clause in cloud firestore? 使用 flutter 检查是否已在 Cloud Firestore 上创建文档 - Checking if document has been created on Cloud Firestore using flutter firebase firestore flutter where查询 - firebase firestore flutter where query 在 flutter 中使用 withConverter 时如何获取 firestore 文档的 documentId? - How to get documentId of firestore document when using withConverter in flutter? Flutter Firebase Auth + Firestore Provider 用户文档 - Flutter Firebase Auth + Firestore Provider user document
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM