- 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:
- Using Map Literal
- 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.
Learn more about Dart and Flutter
- List in Dart
- Abstract class and Abstract Methods in Dart
- Interface in Dart
- Constructors in Dart
- Arrow Function in Dart
- User Defined Function in Dart
- Functions in Dart
- Switch case in Dart
- Conditionals in Dart
- Operators in Dart
- Keywords in Dart
- Variables in Dart
- Data Types in Dart
Top comments (0)