9.4 C
New York
Saturday, December 2, 2023

Flutter & Dart: every() method examples

A few examples of using the every() method (of the Iterable class) in Dart (and Flutter as well). The purpose of this method is to check if every element of a given iterable satisfies one or multiple conditions (with a test function):

bool every(
   bool test(
     E element

1. Check whether every number in a list is exactly divisible by 3:Advertisements

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



2. Check whether every 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;



AdvertisementsYou can find more details about the every() method in the official docs.

Further reading:

You can also take a tour around our Flutter topic page and Dart topic page to see the latest tutorials and examples.


Related Articles

Latest Articles