如何在颤抖中过滤列表? [英] How to filter list in flutter?
本文介绍了如何在颤抖中过滤列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将过滤器添加到列表
I'm trying to add a filter to my list
这是我的工作
productTemp.sort((a, b) => b.productPrice.compareTo(a.productPrice));
productTemp = productTemp.where((x) =>
x.productName.toLowerCase().contains(inputText.toLowerCase()));
productTemp = productTemp.toSet().toList();
此处是我的设置方式
List<ProductsModel> productTemp = [];
然后此
class ProductsModel {
final String productId;
final String productName;
final String isNew;
final String isHot;
final String productImage;
final String categoryId;
final String productPrice;
final String productDescription;
ProductsModel(
{this.productId,
this.productName,
this.isNew,
this.isHot,
this.productImage,
this.categoryId,
this.productPrice,
this.productDescription});
factory ProductsModel.fromJson(Map<String, dynamic> json) {
return ProductsModel(
productId: json['productId'] as String,
productName: json['productName'] as String,
isNew: json['isNew'] as String,
isHot: json['isHot'] as String,
productImage: json['productImage'] as String,
categoryId: json['categoryId'] as String,
productPrice: json['productPrice'] as String,
productDescription: json['productDescription'] as String,
);
}
bool operator ==(o) =>
o is ProductsModel && productId == o.productId;
int get hashCode => productId.hashCode;
}
所以我收到此错误
[错误:flutter / lib / ui / ui_dart_state.cc(148)]未处理的异常:类型
'WhereIterable'不是类型
的子类型'列表'
[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: type 'WhereIterable' is not a subtype of type 'List'
我该如何解决?预先感谢
how can i fix it ? thanks in advance
推荐答案
您的 productTemp
类型为列表
,但其中
返回可迭代
。
您需要将 where
的输出转换为 List
。
Your productTemp
is of type List
but where
returns Iterable
.
You need to turn the output of where
into a List
.
productTemp.where((x) =>
x.productName.toLowerCase().contains(inputText.toLowerCase())).toList();
这篇关于如何在颤抖中过滤列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文