Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I search a list of classes for a specific property of the class?

Tags:

flutter

dart

I have a class called Product which has two properties.

class Product {
  final String productName;
  final int expiryTime;

  Product(this.productName, this.expiryTime);
}

I then made a list of Products like below:

List<Product> allProducts = [
  Product('Apple', 5),
  Product('Banana', 3),
  Product('Pear', 7),
];

I want to search this list to check if a product is in it. For example, if I check if 'Apple' is in the list allProducts, it should be true.

For other lists I have used list.contains() but I cannot see how to use it for this type of list.

like image 469
MrHarvey Avatar asked Oct 18 '25 03:10

MrHarvey


1 Answers

any returns true if the given condition is met in dart list. Hope it helps

var found = allProducts.any((e) => e.productName == "Apple");
print(found);
like image 115
Zain Zafar Avatar answered Oct 20 '25 20:10

Zain Zafar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!