Flutter & Dart: every() method example
A few examples of using the every() method ( Iterable class) in Dart (and Flutter) . The purpose of this method is to check whether each element of a given iteration satisfies one or more conditions (using a test
bool every(
bool test(
E element
)
)
1. Check that each number in the list is divisible by 3 :
void main() {
var myList = [0, 3, 6, 9, 18, 21, 81, 120];
bool result1 = myList.every((element){
if(element %3 ==0){
return true;
} else {
return false;
}
});
// expectation: true
print(result1);
}
output:
true
2. Check that each name in the list contains the “m” character :
void main() {
var names = ['Obama', 'Trump', 'Biden', 'Pompeo'];
bool hasM = names.every((element) {
if(element.contains('m')) return true;
return false;
});
print(hasM);
}
output:
false
find more details about every() method in the official documentation.