Map in Dart

Map In Dart

In a Map, data is stored as keys and values. In Map, each key must be unique. They are similar to HashMaps and Dictionaries in other languages.

How To Create Map In Dart

Here we are creating a Map for String and String. It means keys and values must be the type of String. You can create a Map of any kind as you like.

void main(){
Map<String, String> countryCapital = {
  'USA': 'Washington, D.C.',
  'India': 'New Delhi',
  'China': 'Beijing'
};
  print(countryCapital);
}

Show Output
Run Online

Info

Note: Here Usa, India, and China are keys, and it must be unique.

Access Value From Key

You can find the value of Map from its key. Here we are printing Washington, D.C. by its key, i.e., USA.

void main(){
Map<String, String> countryCapital = {
  'USA': 'Washington, D.C.',
  'India': 'New Delhi',
  'China': 'Beijing'
};
  print(countryCapital["USA"]);
}

Show Output
Run Online

Map Properties In Dart

Properties Work
keys To get all keys.
values To get all values.
isEmpty Return true or false.
isNotEmpty Return true or false.
length It returns the length of the Map.

Example Of Map Properties In Dart

This example finds all keys/values of Map, the first and last element, checks whether it is empty or not, and finds its length.

void main() {
 
  Map<String, double> expenses = {
    'sun': 3000.0,
    'mon': 3000.0,
    'tue': 3234.0,
  };
  
  print("All keys of Map: ${expenses.keys}");
  print("All values of Map: ${expenses.values}");
  print("Is Map empty: ${expenses.isEmpty}");
  print("Is Map not empty: ${expenses.isNotEmpty}");
  print("Length of map is: ${expenses.length}");
}

Show Output
Run Online

Adding Element To Map

If you want to add an element to the existing Map. Here is the way for you:

void main(){
Map<String, String> countryCapital = {
  'USA': 'Washington, D.C.',
  'India': 'New Delhi',
  'China': 'Beijing'
};
  // Adding New Item
  countryCapital['Japan'] = 'Tokio';
  print(countryCapital);
}

Show Output
Run Online

Updating An Element Of Map

If you want to update an element of the existing Map. Here is the way for you:

void main(){
Map<String, String> countryCapital = {
  'USA': 'Nothing',
  'India': 'New Delhi',
  'China': 'Beijing'
};
  // Updating Item
  countryCapital['USA'] = 'Washington, D.C.';
  print(countryCapital);
}

Show Output
Run Online

Map Methods In Dart

Some useful Map methods in dart.

Properties Work
keys.toList() Convert all Maps keys to List.
values.toList() Convert all Maps values to List.
containsKey(‘key’) Return true or false.
containsValue(‘value’) Return true or false.
clear() Removes all elements from the Map.
removeWhere() Removes all elements from the Map if condition is valid.

Convert Maps Keys & Values To List

Let’s convert keys and values of Map to List.

void main() {
 
  Map<String, double> expenses = {
    'sun': 3000.0,
    'mon': 3000.0,
    'tue': 3234.0,
  };
  
  // Without List
  print("All keys of Map: ${expenses.keys}");
  print("All values of Map: ${expenses.values}");
 
  // With List
  print("All keys of Map with List: ${expenses.keys.toList()}");
  print("All values of Map with List: ${expenses.values.toList()}");
  
}

Show Output
Run Online

Check Map Contains Specific Key/Value Or Not?

Let’s check whether the Map contains a specific key/value in it or not.

void main() {
 
  Map<String, double> expenses = {
    'sun': 3000.0,
    'mon': 3000.0,
    'tue': 3234.0,
  };
  
  // For Keys
  print("Does Map contain key sun: ${expenses.containsKey("sun")}");
  print("Does Map contain key abc: ${expenses.containsKey("abc")}");
 
  // For Values
  print("Does Map contain value 3000.0: ${expenses.containsValue(3000.0)}");
  print("Does Map contain value 100.0: ${expenses.containsValue(100.0)}");
  
}

Show Output
Run Online

Removing Items From Map

Suppose you want to remove an element of the existing Map. Here is the way for you:

void main(){
Map<String, String> countryCapital = {
  'USA': 'Nothing',
  'India': 'New Delhi',
  'China': 'Beijing'
};
  
  countryCapital.remove("USA");
  print(countryCapital);
}

Show Output
Run Online

Looping Over Element Of Map

You can use any loop in Map to print all keys/values or to perform operations in its keys and values.

void main(){

  Map<String, dynamic> book = {
    'title': 'Misson Mangal',
    'author': 'Kuber Singh',
    'page': 233
  };
  
 // Loop Through Map
  for(MapEntry book in book.entries){
    print('Key is ${book.key}, value ${book.value}');
  }
}

Show Output
Run Online

Looping In Map Using For Each

In this example, you will see how to use a loop to print all the keys and values in Map.

void main(){

  Map<String, dynamic> book = {
    'title': 'Misson Mangal',
    'author': 'Kuber Singh',
    'page': 233
  };
  
  
 // Loop Through For Each
  book.forEach((key,value)=> print('Key is $key and value is $value'));
  
}

Show Output
Run Online

Remove Where In Dart Map

In this example, you will see how to get students whose marks are greater or equal to 32 using where method.

void main() {
  Map<String, double> mathMarks = {
    "ram": 30,
    "mark": 32,
    "harry": 88,
    "raj": 69,
    "john": 15,
  };
  mathMarks.removeWhere((key, value) => value < 32);
  print(mathMarks);
}

Show Output
Run Online

Video

Watch our video on the map in Dart.