Dart Map()
Syntax & Examples


Syntax of Map

The syntax of Map.Map constructor is:

Map()

This Map constructor of Map creates an empty LinkedHashMap.



✐ Examples

1 Create an empty LinkedHashMap without specifying key-value types

In this example,

  1. We create an empty LinkedHashMap by calling Map() without specifying key-value types.
  2. The resulting emptyMap variable is empty, as it has no key-value pairs.
  3. We print the emptyMap to standard output.

Dart Program

void main() {
  var emptyMap = Map();
  print('Empty map: $emptyMap');
}

Output

Empty map: {}

2 Create an empty LinkedHashMap with integer keys and string values

In this example,

  1. We create an empty LinkedHashMap with key type int and value type String by calling Map().
  2. The resulting emptyMap variable is empty, as it has no key-value pairs.
  3. We print the emptyMap to standard output.

Dart Program

void main() {
  var emptyMap = Map<int, String>();
  print('Empty map: $emptyMap');
}

Output

Empty map: {}

3 Create an empty LinkedHashMap with string keys and dynamic values

In this example,

  1. We create an empty LinkedHashMap with key type String and value type dynamic by calling Map().
  2. The resulting emptyMap variable is empty, as it has no key-value pairs.
  3. We print the emptyMap to standard output.

Dart Program

void main() {
  var emptyMap = Map<String, dynamic>();
  print('Empty map: $emptyMap');
}

Output

Empty map: {}

Summary

In this Dart tutorial, we learned about Map constructor of Map: the syntax and few working examples with output and detailed explanation for each example.