loading...
Cover image for Maps in Dart

Maps in Dart

jay_tillu profile image Jay Tillu😎 ・3 min read
  • 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,
};

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}

Syntax for Adding value to Map at Runtime

mapName[key] = value;

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}

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

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}

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.

For More Information Please Visit Following Links

Wanna get in touch with me? Here are links. I’ll love to become your friend. 😊
Twitter
Facebook
Instagram
Medium
or just mail me at jayviveki13@gmail.com

Posted on by:

jay_tillu profile

Jay Tillu😎

@jay_tillu

🌏 विधायास्य धमॅस्य संरक्षणम् 🌍 ☀️ Society before Self ☀️ 🙏🏻 Humanism by Religion 🙏🏻 💻 Work for Mankind 💻 🌐 Believes in वसुधैव कुटुम्बकम् 🌐

Discussion

pic
Editor guide