簡體   English   中英

無法獲取有關在 flutter firebase(雲 Firestore)中使用何處的文檔

[英]Unable to get document on using where in in flutter firebase (cloud firestore)

我正在嘗試根據用戶設置的價格范圍過濾我的產品。 為此,我使用的范圍是 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;
              });
            },
          ),

和我獲取數據的代碼:-

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 作為默認值進行檢查。

PS:- 我需要創建任何索引嗎? 如果是這樣,那么它的價值是什么?

whereIn檢查與您提供的值是否完全匹配。

你想要做的是:

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

對於您關於索引的問題:如果您需要創建一個索引,firestore 可能會告訴您並給您一個 ling 自動創建它,所以我不會擔心。

另一個注意事項:我認為您檢索數據的方法會導致對數據庫的大量調用。 也許更好的解決方案是僅在用戶停止更新 slider 值時才獲取數據。 這是實現該目標的方法:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM