Maps in Dart
In Dart, a Map is a growable collection which means it can shrink and grow at runtime. The map can contain a NULL value as well.
Just like a List, a 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 which means it can shrink and grow at runtime. The map can contain a NULL value as well.
There are two ways to declare maps:
Using Map Literal
Using Map constructor
Using Map Literal
Just like we declare a list using the var keyword, we can also use var for declaring Maps.
The main difference between declarations is, that for declaring lists we use [](square brackets), but to declare maps we have to use {}(curly braces).
for declaring a 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 is nothing wrong if you declare a map using the 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}
Conclusion
The article discusses Maps in Dart, which is another type of collection used for storing data. Maps store data in key-value pairs, and both keys and values can have any data type. Here are the key points:
Map: In Dart, a Map is a collection where data is stored in key-value pairs. Maps are growable collections, which means they can change in size during runtime. They can also contain null values.
Declaration: There are two ways to declare Maps in Dart:
Using Map Literal: You can declare a Map using a variable and curly braces
{}
. For example:var myMap = {'key1': 'value1', 'key2': 'value2'};
Using Map Constructor: Another way to declare a Map is by using the
Map()
constructor. This method provides flexibility in how you create and initialize the Map.
Maps are a versatile data structure in Dart, allowing you to associate keys with values and efficiently store and retrieve data in your programs.
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 hesitant 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.