gpt4 book ai didi

flutter - 如何在 flutter 中过滤列表?

转载 作者:行者123 更新时间:2023-12-03 02:49:09 25 4
gpt4 key购买 nike

我正在尝试向我的 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;
}

所以我收到这个错误

[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: type 'WhereIterable' is not a subtype of type 'List'



我该如何解决?提前致谢

最佳答案

您的 productTemp类型为 List但是 where返回 Iterable .
你需要把where的输出转成List .

productTemp.where(
(x) => x.productName.toLowerCase().contains(inputText.toLowerCase())
).toList();

关于flutter - 如何在 flutter 中过滤列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57270015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com