Map containsValue()

Map containsValue() method checks if the given value is present in this Map. The method returns a boolean value of true if the given value is present in this Map, or false if not.

Syntax

The syntax to check if the value value is present in the Map map1 is

map1.containsValue(value);

The method returns boolean value.

ADVERTISEMENT

Examples

Check if the value=25 is present in Map

In the following Dart Program, we take a Map map1 and check if the value 25 is present in this Map map1, using Map.containsValue() method.

main.dart

void main() {
    var map1 = {'apple': 25, 'banana': 10, 'cherry': 6};
    var value = 25;

    if ( map1.containsValue(value) ) {
        print('The value is present in this Map.');
    } else {
        print('The value is not present in this Map.');
    }
}

Output

The value is present in this Map.

Since, the given value is present in this Map, containsValue() returned true.

Check if the value=40 is present in Map

In the following Dart Program, we take a Map map1 and check if the value 40 is present in this Map map1, using Map.containsValue() method.

main.dart

void main() {
    var map1 = {'apple': 25, 'banana': 10, 'cherry': 6};
    var value = 40;

    if ( map1.containsValue(value) ) {
        print('The value is present in this Map.');
    } else {
        print('The value is not present in this Map.');
    }
}

Output

The value is not present in this Map.

Since the given value is not present in this Map, containsValue() returned false.

Conclusion

In this Dart Tutorial, we learned how to check if the given value is present in this Map, using Map.containsValue() method.