Dart – If any Element in List satisfies given Condition

To check if any of the elements from a List satisfy a given test/condition in Dart, call any() method on this list and pass the test function as argument. any() method returns true if at least one of the elements return true for given test function, or else it returns false if all of the elements returns false for given test function.

The test function must take the element as argument and return a boolean value.

Syntax

The syntax to call any() method on the List list with test function test passed as argument is

list.any(test)

Example

In this example, we take a list of integers, and check if there is at least one even number in this list, using List.any() method. The test function is a lambda function that takes an integer and returns true if the integer is even number, or false if not.

main.dart

bool isEven(int n) => (n % 2 == 0);

void main() {
  var list = [1, 4, 7, 15, 32];
  var result = list.any(isEven);
  print('Is there any even number in list :  $result');
}

Output

Is there any even number in list :  true

Conclusion

In this Dart Tutorial, we learned how to check if at least one element from the list satisfies given test function, with examples.