Map – Dart Programming

Last Updated On Friday 14th Jan 2022

Map in Dart

Map is an object that consists of a key and a value.

  • In Map , key must be of type String and value can be dynamic.

There are two ways to declare a map

  • Map Literal
  • Map Constructor

Map Literal

  • With the Map Literal, you need to use a pair of curly brackets . Here’s an example of the syntax.
	var names = { key1 : value1, key2 : value2 }
	

In Above Code there are key1 and key2 as keys in the map, and while value1 and value2 as the value of the keyword map.

	void main() {
  var appName = {'name': 'Blog App', 'platform': 'Flutter-Android'};
  print(appName);
}
	
	{name: Blog App, platform: Flutter-Android}
	

Dart Map Example

You Can Specific key,Use the following syntax

	void main() {
  var appName = {'name': 'Blog App', 'platform': 'Flutter-Android'};
  print(appName['name']);
}
	
	Blog App
	

Dart Maps

Constructor

It takes two steps to construct map, namely declaring and Initializing.

	var name = new Map()
name[key]=value
	

In Above Code , we initializing the map variable and adding map elements.

	void main() {
  var appName = new Map();
  appName['name'] = 'News App';
  appName['platform'] = 'Flutter-Windows';
  print(appName);
}
	

flutter map

Various properties have been provided with Dart Map.

  • keys – Returns all keys
  • values – Returns all values
  • length – Returns the length on the Map
  • isEmpty – Returns true if the map element is empty, false otherwise
  • isNotEmpty – Returns true if the map element is not empty. false otherwise.
	void main() {
  var appName = new Map();
  appName['name'] = 'News App';
  appName['platform'] = 'Flutter-Windows';
  appName['search'] = true;
}
	
	  print(appName.keys);
  print(appName.values);
  print(appName.isEmpty);
  print(appName.isNotEmpty);
  print(appName.length);
	

The OutPut of above code

	(name, platform, search)
(News App, Flutter-Windows, true)
false
true
3