The syntax of List.contains() method is:
bool contains(Object? element)
This contains() method of List whether the collection contains an element equal to element
.
Parameter | Optional/Required | Description |
---|---|---|
element | required | the element to check for in the collection |
List.contains() returns value of type bool
.
In this example,
numbers
containing the numbers 1, 2, 3, 4, 5
.contains()
method to check if numbers
contains the number 3
.3
is contained in numbers
, is printed to standard output.void main() {
List<int> numbers = [1, 2, 3, 4, 5];
bool containsThree = numbers.contains(3);
print('List contains 3: $containsThree'); // Output: List contains 3: true
}
List contains 3: true
In this example,
characters
containing the characters 'a', 'b', 'c', 'd'
.contains()
method to check if characters
contains the character 'd'
.'d'
is contained in characters
, is printed to standard output.void main() {
List<String> characters = ['a', 'b', 'c', 'd'];
bool containsD = characters.contains('d');
print('List contains \'d\': $containsD'); // Output: List contains 'd': true
}
List contains 'd': true
In this example,
fruits
containing the strings 'apple', 'banana', 'orange'
.contains()
method to check if fruits
contains the string 'pear'
.'pear'
is contained in fruits
, is printed to standard output.void main() {
List<String> fruits = ['apple', 'banana', 'orange'];
bool containsPear = fruits.contains('pear');
print('List contains \'pear\': $containsPear'); // Output: List contains 'pear': false
}
List contains 'pear': false
In this Dart tutorial, we learned about contains() method of List: the syntax and few working examples with output and detailed explanation for each example.