Dart .contains()

Anonymous contributor's avatar
Anonymous contributor
Published Feb 14, 2024

The .contains() method determines whether a specified element is present in a list. It returns a Boolean value of true if the element is found in the list and false if it happens otherwise.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours

Syntax

list.contains(element)
  • list: The name of the list that needs to be searched.
  • element: The element that needs to be searched.

Example

In this example, the .contains() method is used to check if the string banana is present in the fruits list:

void main() {
List<String> fruits = ['apple', 'banana', 'orange'];
// The .contains() method searches for the element 'banana' in the 'fruits' list
bool containsBanana = fruits.contains('banana');
print(containsBanana);
}

Here is the output for the above code:

true

All contributors

Learn Dart on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours