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

ParameterOptional/RequiredDescription
valuerequiredthe 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,

  1. We create a map named ages containing ages of people.
  2. We use the containsValue() method on ages to check if it contains the value 25.
  3. Since the map contains the value 25, containsValue() returns true.
  4. 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,

  1. We create a map named grades containing grades.
  2. We use the containsValue() method on grades to check if it contains the value 'D'.
  3. Since the map does not contain the value 'D', containsValue() returns false.
  4. 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,

  1. We create a map named flags containing boolean flags.
  2. We use the containsValue() method on flags to check if it contains the value true.
  3. Since the map contains the value true, containsValue() returns true.
  4. 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.