Dart – Common Collection Methods

List, Set, and Map share common functionality found in many collections. Some of this common functionality is defined by the Iterable class, which List and Set implement.

1. isEmpty() or isNotEmpty:

Use isEmpty or isNotEmpty to check whether a list, set, or map has items:

Example:

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

void main(){
    
var coffees = [];
var teas = ['green', 'black', 'chamomile', 'earl grey'];
print(coffees.isEmpty);
print(teas.isNotEmpty);
}

chevron_right


Output:



true
true

2. forEach():

To apply a function to each item in a list, set, or map, you can use forEach():

Example:

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

void main(){
    
    
var teas = ['green', 'black', 'chamomile', 'earl grey'];
  
var loudTeas = teas.map((tea) => tea.toUpperCase());
loudTeas.forEach(print);
}

chevron_right


Output:

GREEN
BLACK
CHAMOMILE
EARL GREY

3.where():

Use Iterable’s where() method to get all the items that match a condition. Use Iterable’s any() and every() methods to check whether some or all items match a condition.

Example:

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

void main(){
    
var teas = ['green', 'black', 'chamomile', 'earl grey'];
  
// Chamomile is not caffeinated.
bool isDecaffeinated(String teaName) =>
    teaName == 'chamomile';
  
// Use where() to find only the items that return true
// from the provided function.
  
  
// Use any() to check whether at least one item in the
// collection satisfies a condition.
print(teas.any(isDecaffeinated));
  
// Use every() to check whether all the items in a
// collection satisfy a condition.
print(!teas.every(isDecaffeinated));
}

chevron_right


Output:

true
true



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.