Related Articles

Related Articles

Dart Programming – Map
  • Difficulty Level : Basic
  • Last Updated : 23 Dec, 2020

In Dart programming, Maps are dictionary-like data types that exist in key-value form (know as lock-key). There is no restriction on the type of data that goes in a map data type. Maps are very flexible and can mutate their size based on the requirements. However, it is important to note that all locks (keys) need to be unique inside a map data type.

We can declare Map in two ways: 

  1. Using Map Literals
  2. Using Map Constructors

Map Literals:

Map can be declared using map literals as shown below: 

Syntax: 
// Creating the Map using Map Literals
var map_name = { key1 : value1, key2 : value2, ..., key n : value n }

Example 1: 

Creating Map using Map Literals 
 



Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

void main() {  
  // Creating Map using is literals
  var gfg = {'position1' : 'Geek', 'position2' : 'for', 'position3' : 'Geeks'};
    
  // Printing Its content
  print(gfg);
    
  // Printing Specific Content
  // Key is defined
  print(gfg['position1']); 
    
  // Key is not defined
  print(gfg[0]); 
}

chevron_right


Output: 

{position1: Geek, position2: for, position3: Geeks}
Geek
null

Example 2: 
 

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

void main() {  
  // Creating Map using is literals
  var gfg = {'position1' : 'Geek' 'for' 'Geeks'};
    
  // Printing Its content
  print(gfg);
    
  // Printing Specific Content
  // Key is defined
  print(gfg['position1']); 
}

chevron_right


Output: 

{position1: GeekforGeeks}
GeekforGeeks

You have noticed that different strings get concatenated to one.

Example 3: 

Inserting a new value into Map 
 



Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

void main() {  
  // Creating Map
  var gfg = {'position1' : 'Geeks' 'for'  'Geeks'};
    
  // Printing Its content before insetion
  print(gfg);
    
  // Inserting a new vaplue in Map
  gfg ['position0'] = 'Welcome to ';
    
  // Printing Its content after insertion
  print(gfg);
    
  // Printing Specific Content
  // Keys is defined
  print(gfg['position0'] + gfg['position1']); 
}

chevron_right


Output: 

{position1: GeeksforGeeks}
{position1: GeeksforGeeks, position0: Welcome to }
Welcome to GeeksforGeeks

Map Constructors:

Syntax: 
// Creating the Map using Map Constructor
var map_name = new Map();
// Assigning value and key inside Map
map_name [ key ] = value;

Example 1:

Creating Map using Map Constructors 
 

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

void main() {  
  // Creating Map using Contructors
  var gfg = new Map();
    
  // Inserting values into Map
  gfg [0] = 'Geeks';
  gfg [1] = 'for';
  gfg [2] = 'Geeks';
    
  // Printing Its content
  print(gfg);
    
  // Printing Specific Content
  // Key is defined
  print(gfg[0]); 
}

chevron_right


Output: 

{0: Geeks, 1: for, 2: Geeks}
Geeks

Example 2: 

Assigning same key to different element 
 

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

void main() {  
  // Creating Map using Contructors
  var gfg = new Map();
    
  // Inserting values into Map
  gfg [0] = 'Geeks';
  gfg [0] = 'for';
  gfg [0] = 'Geeks';
    
  // Printing Its content
  print(gfg);
    
  // Printing Specific Content
  // Key is defined
  print(gfg[0]); 
}

chevron_right


Output: 

{0: Geeks}
Geeks

You have noticed that the other two values were simply ignored.
 

My Personal Notes arrow_drop_up
Recommended Articles
Page :