DEV Community

Cover image for Maps in Dart
Jay Tillu
Jay Tillu

Posted on • Edited on

Maps in Dart

  • Just like List, Map is also a type of collection. In Maps data is stored in key: value pairs. Keys and values can be of any type.
  • The map is a growable collection that means it can shrink and grow at runtime.
  • Map can contain NULL value as well.
  • There are two ways to to declare maps:
  1. Using Map Literal
  2. Using Map constructor

Using Map Literal

  • Just like we declare list using var keyword, we can also use var for declaring Maps.

  • The main difference between declaration is, for declaring list we use , but to declare maps we have to use {}(curly braces).

  • for declaring list use [ ]

  • for declaring map use {}

Syntax for declaring map through variable

var VariableName = {
  key : value, 
  key : value,
  key : value,
  key : value,
};
Enter fullscreen mode Exit fullscreen mode

Sample code for declaring Map through variable

void main() {
  var myMap = {
    "id": "jay",
    "password": "1234",
    "name": "Jay Tillu",
  };
  print(myMap);
}

output
{id: jay, password: 1234, name: Jay Tillu}
Enter fullscreen mode Exit fullscreen mode

Syntax for Adding value to Map at Runtime

mapName[key] = value;
Enter fullscreen mode Exit fullscreen mode

Sample code for adding value to Map

void main() {
  var myMap = {
    "id": "jay",
    "password": "1234",
    "name": "Jay Tillu",
  };
  print("************ Before adding data in Map ************");
  print(myMap);

  // Adding value to Map

  myMap["country"] = "india";
  print("************ After adding data in Map ************");
  print(myMap);
}

Output
************ Before adding data in Map ************
{id: jay, password: 1234, name: Jay Tillu}
************ After adding data in Map ************
{id: jay, password: 1234, name: Jay Tillu, country: india}
Enter fullscreen mode Exit fullscreen mode

Map Constructor

  • For declaring Map we can also use Map() constructor. It’s just which way you like. There nothing wrong if you declare map using standard method.

Syntax for declaring map through Map constructor

var mapName = Map();
mapName[key] = value;   // For adding value to your Map
Enter fullscreen mode Exit fullscreen mode

Sample Code for declaring map through Map constructor

void main() {
  var myMap = Map();
  print("************ Before adding data in Map ************");
  print(myMap);

//  Adding value to Map
  myMap["id"] = "jay";
  myMap["password"] = "1234";
  myMap["country"] = "India";
  print("************ After adding data in Map ************");
  print(myMap);
}

Output
************ Before adding data in Map ************
{}
************ After adding data in Map ************
{id: jay, password: 1234, country: India}
Enter fullscreen mode Exit fullscreen mode

So, guys, That’s it for maps. Feel free to let me know if I miss something. Till then Keep Loving, Keep Coding. And Just like always I’ll catch you up in the next article. 😊

Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it. Don’t feel hesitate that you can’t do that or you don’t know this concept or that concept. Remember every programmer was passed from the path on which you are walking right now. Remember Every Master was Once a Beginner. Work hard and Give your best.

Learn more about Dart and Flutter

Follow me for more such content

Top comments (0)