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:
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.