Flutter 如何在类列表中搜索该类的特定属性?
我有一个名为Flutter 如何在类列表中搜索该类的特定属性?,flutter,dart,Flutter,Dart,我有一个名为Product的类,它有两个属性 class Product { final String productName; final int expiryTime; Product(this.productName, this.expiryTime); } 然后我列出了一个产品的列表,如下所示: List<Product> allProducts = [ Product('Apple', 5), Product('Banana', 3), Prod
Product
的类,它有两个属性
class Product {
final String productName;
final int expiryTime;
Product(this.productName, this.expiryTime);
}
然后我列出了一个产品的列表,如下所示:
List<Product> allProducts = [
Product('Apple', 5),
Product('Banana', 3),
Product('Pear', 7),
];
列出所有产品=[
产品('Apple',5),
产品(“香蕉”,3),
产品(“梨”,7),
];
我想搜索此列表以检查其中是否有产品。例如,如果我检查“Apple”是否在列表中allProducts
,它应该是true
对于其他列表,我使用了list.contains()
,但我看不出如何将其用于此类列表。您可以执行以下操作:
allProducts.forEach((res){
if(res.productName == "Apple"){
print("True");
}
});
在列表中迭代,然后检查res.productName
是否等于Apple如果dart列表中满足给定条件,则任何返回true
。希望能有帮助
var-found=allProducts.any((e)=>e.productName==“苹果”);
打印(已找到);
Zain,你也能看看我的问题吗?我正在用Flatter处理事情,无法理解我现在做错了什么-如果代码上有任何提示,我将不胜感激。请让我检查一下