The syntax of Map.containsValue() method is:
bool containsValue(Object? value)
This containsValue() method of Map whether this map contains the given value
.
Parameter | Optional/Required | Description |
---|---|---|
value | required | the value to check for in the map |
Map.containsValue() returns value of type bool
.
In this example,
ages
containing ages of people.containsValue()
method on ages
to check if it contains the value 25.containsValue()
returns true
.void main() {
Map<String, int> ages = {'John': 30, 'Jane': 25, 'Doe': 35};
bool contains25 = ages.containsValue(25);
print('Contains value 25 in ages: $contains25');
}
Contains value 25 in ages: true
In this example,
grades
containing grades.containsValue()
method on grades
to check if it contains the value 'D'.containsValue()
returns false
.void main() {
Map<int, String> grades = {1: 'A', 2: 'B', 3: 'C'};
bool containsD = grades.containsValue('D');
print('Contains value "D" in grades: $containsD');
}
Contains value "D" in grades: false
In this example,
flags
containing boolean flags.containsValue()
method on flags
to check if it contains the value true
.true
, containsValue()
returns true
.void main() {
Map<String, bool> flags = {'isReady': true, 'isEnabled': false};
bool containsTrue = flags.containsValue(true);
print('Contains value true in flags: $containsTrue');
}
Contains value true in flags: true
In this Dart tutorial, we learned about containsValue() method of Map: the syntax and few working examples with output and detailed explanation for each example.