Map

The Map object is a key and value pair. Keys and values on a map may be of any type. It is a dynamic collection.

Declare Map in Dart

While Declaring Map there can be only two cases one where declared Map is empty and another where declared Map contains elements in it. Both Cases are mentioned below:

1. Declaring Empty Map

// Method 1
Map? map_name;

// Method 2
Map<key_datatype , value_datatype>? map_name;

// Method 3
var map_name = new Map();

2. Declaring Map with Elements inside it.

// Method 1
Map x={
key1 : value1;
key2 : value2;
};

// Method 2
Map<key_datatype , value_datatype> map_name{
key1 : value1;
key2 : value2;
};

// Method 3
var map_name{
key1 : value1;
key2 : value2;
};

Below is the implementation of Map in Dart:

Dart
void main() { 
      Map gfg = new Map(); 
      gfg['First'] = 'Geeks'; 
      gfg['Second'] = 'For'; 
      gfg['Third'] = 'Geeks';
      print(gfg); 
}  

 Output: 

{First: Geeks, Second: For, Third: Geeks}

Note: If the type of a variable is not specified, the variable’s type is dynamic. The dynamic keyword is used as a type annotation explicitly.

Dart – Data Types

Like other languages (C, C++, Java), whenever a variable is created, each variable has an associated data type. In Dart language, there are the types of values that can be represented and manipulated in a programming language. 

In this article, we will learn about Dart Programming Language Data Types.

Similar Reads

Data Types in Dart

The data type classification is as given below:...

1. Number

The number in Dart Programming is the data type that is used to hold the numeric value. Dart numbers can be classified as:...

2. String

It used to represent a sequence of characters. It is a sequence of UTF-16 code units. The keyword string is used to represent string literals. String values are embedded in either single or double-quotes....

3. Boolean

It represents Boolean values true and false. The keyword bool is used to represent a Boolean literal in DART....

4. List

List data type is similar to arrays in other programming languages. A list is used to represent a collection of objects. It is an ordered group of objects....

5. Map

The Map object is a key and value pair. Keys and values on a map may be of any type. It is a dynamic collection....

Click Here to Check Dart Tutorial to Learn More about Dart

...