Maps in Dart
Tags: dart, todayilearned, beginners
Published: 2019-09-16 03:03:39

* 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 [](square brackets), but to declare maps we have to use {}(curly braces).

* **for declaring list use [ ]**
* **for declaring map use {}**

####Syntax for declaring map through variable

```dart
var VariableName = {
  key : value, 
  key : value,
  key : value,
  key : value,
};
```

####Sample code for declaring Map through variable

```dart
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

```dart
mapName[key] = value;
```

####Sample code for adding value to Map

```dart
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

```dart
var mapName = Map();
mapName[key] = value;   // For adding value to your Map
```

####Sample Code for declaring map through Map constructor

```dart
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
* [Fuchsia OS official site](https://fuchsia.dev/)
* [Dart official site](https://dart.dev/)
* [Flutter official site](https://flutter.dev/)


Jai Hind, Vande Mataram 🇮🇳

Wanna get in touch with me? Here are links. I’ll love to become your friend. 😊

- [My Site] (https://www.jaytillu.in)
- [My Blogs] (https://blogs.jaytillu.in)
- [Twitter](https://twitter.com/jay_tillu)
- [Facebook](https://www.facebook.com/jaytillu.1314/)
- [Instagram](https://www.facebook.com/jaytillu.1314/)
- [Medium](https://medium.com/jay-tillu)
- [LinkedIn](https://www.linkedin.com/in/jaytillu/)
- [Github](https://www.github.com/jay-tillu)
- [Stack overflow](https://stackoverflow.com/users/8509590/jay-tillu)
or just mail me at jd.tillu@gmail.com