Dart Map containsValue()
Syntax & Examples
Syntax of Map.containsValue()
The syntax of Map.containsValue() method is:
bool containsValue(Object? value)
This containsValue() method of Map whether this map contains the given value
.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
value | required | the value to check for in the map |
Return Type
Map.containsValue() returns value of type bool
.
✐ Examples
1 Check if map contains value 25
In this example,
- We create a map named
ages
containing ages of people. - We use the
containsValue()
method onages
to check if it contains the value 25. - Since the map contains the value 25,
containsValue()
returnstrue
. - We print the result to standard output.
Dart Program
void main() {
Map<String, int> ages = {'John': 30, 'Jane': 25, 'Doe': 35};
bool contains25 = ages.containsValue(25);
print('Contains value 25 in ages: $contains25');
}
Output
Contains value 25 in ages: true
2 Check if map contains value 'D'
In this example,
- We create a map named
grades
containing grades. - We use the
containsValue()
method ongrades
to check if it contains the value 'D'. - Since the map does not contain the value 'D',
containsValue()
returnsfalse
. - We print the result to standard output.
Dart Program
void main() {
Map<int, String> grades = {1: 'A', 2: 'B', 3: 'C'};
bool containsD = grades.containsValue('D');
print('Contains value "D" in grades: $containsD');
}
Output
Contains value "D" in grades: false
3 Check if map contains value true
In this example,
- We create a map named
flags
containing boolean flags. - We use the
containsValue()
method onflags
to check if it contains the valuetrue
. - Since the map contains the value
true
,containsValue()
returnstrue
. - We print the result to standard output.
Dart Program
void main() {
Map<String, bool> flags = {'isReady': true, 'isEnabled': false};
bool containsTrue = flags.containsValue(true);
print('Contains value true in flags: $containsTrue');
}
Output
Contains value true in flags: true
Summary
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.