What is the map.containsKey() method in Dart?

The map.containsKey() function in Dart is used to check if a map contains the specific key sent as a parameter. map.containsKey() returns true if the map contains that key, otherwise it returns false.

Figure 1 below shows a visual representation of the map.containsKey() function:

Figure 1

Syntax

map.containsKey(key)

map is the name of the queue.

Parameter

The map.containsKey() function requires a key in order to check that a specific key-value pair exists in the map.

Return value

The function returns a Boolean value, i.e., true if a specific key is present in a map or false if it is not present.

Code

The following code shows how to use the map.containsKey() function in Dart:

import 'dart:convert';
void main() {
var map = new Map();
map [1] = 'Tom';
map [2] = 'Alsvin';
map [3] = 'Eddie';
//map containg 3 key
print("map contains key number 3: ${map.containsKey(3)}");
//map containg 0 key
print("map contains key number 0: ${map.containsKey(0)}");
}

Free Resources