.every()
Anonymous contributor
Published Aug 28, 2024
Contribute to Docs
The .every()
method iterates through each element in the list and returns a boolean value indicating whether all elements satisfy the given condition.
Syntax
list.every((e) => condition)
list
: The collection (such as aList
) on which the.every()
method is called. This is the list that will be checked against the given condition.e
: Represents each individual element in the list.condition
: The condition that each element is tested against. This is a boolean expression or function that determines whether the.every()
method should returntrue
orfalse
.
Example
The following example shows how .every()
method works:
void main() {// Define a list of integersList<int> numbers = [2, 4, 6, 8];// Use the .every() method to check if all elements in the list are even// The lambda function (e) => e % 2 == 0 returns true if the element is evenbool allEven = numbers.every((e) => e % 2 == 0);// Print the result: "Are all elements even: true"print("Are all elements even: $allEven");}
The output for the above code will be:
Are all elements even: true
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.