Introduction
In this article, we will be learning about the Map in dart. The Map is a data structure that stores the data in the form of key-value pairs means every value is associated with a key. This data structure also resembles the structure of a dictionary, due to which in python it is called a Dictionary. To learn more about python dictionary, go to this link.
In the Map, all the keys should be unique, there is no such constraint on values. A map is a dynamic collection that means it can contract and expands at runtime. We can declare the Map using curly braces {} with a comma (,) separated entries. Now let’s discuss its implementation.
Implementation of Map
A map can be implemented or declared using two ways in dart:
- Using Map Literals
- Using Map Constructor
Implementation and examples of both the ways are given below.
Using Map Literals
In this kind of declaration, we do not just declare the Map instead we initialize it with the data also. Here we assign the key-value pairs separated with a comma (,) and enclosed in the curly braces {}.
Since dart is a type of infer language, means it can automatically know the data type. So we can declare Map using two ways:
Using Explicit Data Types
Here, you cannot change the data type.
Syntax:
Map<int,String> mp = {1:"Ninja1",2:"Ninja2",3:"Ninja3"}
Using var keywords
Here you can change the data type.
Syntax:
var mp = {1:"Ninja1",2:"Ninja2","Third":"Ninja3"};
Example: Creating a map of type var.
Code:
void main() {
var mp = {1:"Ninja1",2:"Ninja2","Third":"Ninja3"};
print(mp);
}
Output:
{1: Ninja1, 2: Ninja2, Third: Ninja3}
Explanation: Since the map is of type var, so it can have keys and values of any data type.
Example: Creating a map of predefined data types.
Code:
void main() {
Map<int,String> mp = {1:"Ninja1",2:"Ninja2",3:"Ninja3"};
print(mp);
}
Output:
{1: Ninja1, 2: Ninja2, 3: Ninja3}
Explanation: Map ‘mp’ will only have the keys of data type int and values of data type String.
Example: In this example, we will try to change the data type of key other than what is defined during the declaration.
Code:
void main() {
//Here changing data type generates error
Map<int,String> mp = {1:"Ninja1",2:"Ninja2","Third":"Ninja3"};
print(mp);
}
Output:
Error compiling to JavaScript:
Info: Compiling with sound null safety
Warning: Interpreting this as package URI, 'package:dartpad_sample/main.dart'.
lib/main.dart:3:47:
Error: A value of type 'String' can't be assigned to a variable of type 'int'.
Map<int,String> mp = {1:"Ninja1",2:"Ninja2","Third":"Ninja3"};
^
Error: Compilation failed.
Explanation: We changed the data type of the key of the third element. It is showing an error because we have declared the key's data type as int.
Example: In this example, we will add an element to the map.
Code:
void main() {
//Adding elements to a map.
var mp = {"S.No.":155, "Name":"Ninja1"};
mp["Branch"] = "CSE";
print(mp);
}
Output:
{S.No.: 155, Name: Ninja1, Branch: CSE}
Explanation: Adding a new element to our map ‘mp’ where the key is “Branch” and the value is “CSE”.
Using Map Constructor
In this kind of declaration, we first declare the map object and then initialize the value to it. Here also, we can use the var keyword or the explicit data types.
Syntax:
Map<String,String> mp = Map();
//OR
var mp = Map();
Example: We will create a map using a map constructor and in the last, we will print the map.
Code:
void main() {
Map<String,String> mp = Map();
mp["Sansa"] = "Stark";
mp["Rhaegar"] = "Targaryen";
mp["Tyrion"] = "Lannister";
print(mp);
}
Output:
{Sansa: Stark, Rhaegar: Targaryen, Tyrion: Lannister}
Explanation: This map has the character's name of GOT where the key is the name of the character and the value is the name of his house.
Example: In this example, we will insert a list as a value.
Code:
void main() {
var mp = Map();
mp["Stark"] = "Sansa";
//Using list inside map.
mp["Targaryen"] = ["Rhaegar","Aegon"];
mp["Lannister"] = "Tyrion";
print(mp);
}
Output:
{Stark: Sansa, Targaryen: [Rhaegar, Aegon], Lannister: Tyrion}
Explanation: This map has house names of GOT where the keys are the houses and the values are member from that house. In the 'Targaryen' house, we have two members which are in the form of a list.





