Best Cosmetic Hospitals Near You

Compare top cosmetic hospitals, aesthetic clinics & beauty treatments by city.

Trusted • Verified • Best-in-Class Care

Explore Best Hospitals

What is the List firstWhere() method in Dart?

Uncategorized
void main(){
  // Creating list languages
  List languages = new List();
  // Adding items to the list
  languages.add('Dart');
  languages.add('Python');
  languages.add('Java');
  languages.add('C#');
  languages.add('C++');
  // Print result
  print(languages);   
 // iterate through the list
 // Returns the first item whose length is greater than 3
  var val = languages.firstWhere((item) => item.length > 3, orElse: () => null);
  // if val is null, then the if statement is executed
  if ( null == val ) {
    print('Language not found.');
  }
  // Display result
  print(val);  

  // Creating another list
  List<String> fruits = ['Apple', 'Lemon', 'Avocado', 'Orange'];
  // iterate through the list
 // Returns the first item that contains PineApple
 // the orElse() is invoke if not found
  var fav = fruits.firstWhere((item) => item.contains('PineApple'), 
      orElse: () => 'Fruit not available');
  print(fav);
 }

Output

 [Dart, Python, Java, C#, C++]
 Dart
 Fruit not available

Best Cardiac Hospitals Near You

Discover top heart hospitals, cardiology centers & cardiac care services by city.

Advanced Heart Care • Trusted Hospitals • Expert Teams

View Best Hospitals
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x