The syntax of List.every() method is:
bool every(bool test(E element))
This every() method of List checks whether every element of this iterable satisfies test
.
Parameter | Optional/Required | Description |
---|---|---|
test | required | a function that takes an element and returns a boolean value |
List.every() returns value of type bool
.
In this example,
numbers
containing the numbers 1, 2, 3, 4, 5
.every()
method to check if all elements in numbers
are greater than 0
.void main() {
List<int> numbers = [1, 2, 3, 4, 5];
bool allPositive = numbers.every((element) => element > 0);
print('All numbers are positive: $allPositive'); // Output: All numbers are positive: true
}
All numbers are positive: true
In this example,
characters
containing the characters 'a', 'b', 'c', 'd'
.every()
method to check if all characters in characters
are lowercase by comparing each character to its lowercase equivalent.void main() {
List<String> characters = ['a', 'b', 'c', 'd'];
bool allLowerCase = characters.every((element) => element == element.toLowerCase());
print('All characters are lowercase: $allLowerCase'); // Output: All characters are lowercase: true
}
All characters are lowercase: true
In this example,
words
containing the strings 'apple', 'banana', 'orange'
.every()
method to check if all words in words
have a length greater than 4
.void main() {
List<String> words = ['apple', 'banana', 'orange'];
bool allLongerThanFour = words.every((element) => element.length > 4);
print('All words are longer than four characters: $allLongerThanFour'); // Output: All words are longer than four characters: false
}
All words are longer than four characters: false
In this Dart tutorial, we learned about every() method of List: the syntax and few working examples with output and detailed explanation for each example.