Dart Set every()
Syntax & Examples
Set.every() method
The `every` method in Dart checks whether every element of this iterable satisfies the provided test function.
Syntax of Set.every()
The syntax of Set.every() method is:
bool every(bool test(E element))
This every() method of Set checks whether every element of this iterable satisfies test.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
test | required | A function that takes an element of the iterable and returns a boolean value. |
Return Type
Set.every() returns value of type bool
.
✐ Examples
1 Checking if all numbers are even
In this example,
- We create a List
numbers
with values [2, 4, 6, 8]. - We use the
every()
method with a test function that checks if all elements are even. - We print the result, which indicates if all numbers in the list are even.
Dart Program
void main() {
List<int> numbers = [2, 4, 6, 8];
bool allEven = numbers.every((element) => element.isEven);
print(allEven);
}
Output
true
2 Checking for long words
In this example,
- We create a List
words
with values ['apple', 'banana', 'cherry']. - We use the
every()
method with a test function that checks if all words have a length greater than 3. - We print the result, which indicates if all words in the list are long.
Dart Program
void main() {
List<String> words = ['apple', 'banana', 'cherry'];
bool allLong = words.every((element) => element.length > 3);
print(allLong);
}
Output
false
Summary
In this Dart tutorial, we learned about every() method of Set: the syntax and few working examples with output and detailed explanation for each example.