• Guides
    • Getting started with Dart
  • Blog
  • FAQ
    • Dart
    • Flutter
  • Contact
  • Guides
    • Getting started with Dart
  • Blog
  • FAQ
    • Dart
    • Flutter
  • Contact
  • Getting Started with Dart
    • Dart Variables
    • Dart Operators
    • Dart Comments
  • Dart Data Types
    • Dart Strings
    • Dart Numbers
    • Dart Booleans
    • Dart Lists
    • Dart Maps
  • Control Flow
    • Dart if...else Statements
    • Dart switch Statement
    • Dart for Loop
    • Dart for...in Loop
    • Dart while and do...while Loops
    • break and continue Statements
  • Dart Functions
  • Dart Classes
  • Dart Objects
  • Dart Constructors
  • Dart Exceptions

Dart Maps

191 views 2

Written by Ajaya Shrestha
January 4, 2022

In this chapter, we will learn about dart maps data types and the properties and methods of map that are available in dart.


Dart Map

In dart, a map denote a collection of data in key/value pairs. The key and value can be of any data type. For example,

var person = {'name': 'Phil', 'age': 25, 'isMale': true};

Declaring Map

We can create a map data type in two ways.

1. Using map literal

2. Using map constructor

Using map literal

We can create map data type by enclosing the value in curly braces and separating each pair by comma. For example,

void main() {
var person = {'name': 'Phil', 'age': 25, 'isMale': true};
print(person);
}

Output

{name: Phil, age: 25, isMale: true}

Using map Constructor

Another way to create map data is by passing the new keyword to the Map constructor. For example,

void main() { 
   var person = new Map(); 
   person['name'] = 'Phil'; 
   person['age'] = 25;
   person['isMale'] = true;
   print(person); 
}

Output

{name: Phil, age: 25, isMale: true}

Note: In map data types, keys must be unique.


Dart Map Properties

Maps data types provide various properties. Some of them are:

PropertiesDescription
lengthReturns the number of key/value pairs in the map
valuesReturns an iterable object representing values
keysReturns an iterable object representing keys
isEmptyReturns true if the map is an empty map
isNotEmptyReturns false if the map is an empty Map

Map Properties Examples

void main() {
var person = {'name': 'Phil', 'age': 25, 'isMale': true};
print(person);

print(person.length);
print(person.isEmpty);
print(person.isNotEmpty);
print(person.keys);
print(person.values);
}

Output

{name: Phil, age: 25, isMale: true}
3
false
true
(name, age, isMale)
(Phil, 25, true)

Dart Map Methods

Maps data type also has multiple methods available. Some of them are:

MethodsDescription
clear()Removes all pairs from the map
addAll()Adds all key-value pairs of other to this map
remove()Removes key and its associated value

Map Method Examples

void main() {
var person = {'name': 'Phil', 'age': 25, 'isMale': true};
print(person);

person.addAll({'address': 'New York', 'email': '[email protected]'});
print('After adding: ${person}');

person.remove('age');
print('After removing: ${person}');

person.clear();
print('After clearing: ${person}');
}

Output

{name: Phil, age: 25, isMale: true}
After adding: {name: Phil, age: 25, isMale: true, address: New York, email: [email protected]}
After removing: {name: Phil, isMale: true, address: New York, email: [email protected]}
After clearing: {}

Was this helpful?

2 Yes  No
Related Articles
  • Dart Constructors
  • Dart Exceptions
  • Dart Objects
  • Dart Classes
  • Control Flow
  • Dart Functions
Previously
Dart Lists
Up Next
Control Flow
flutter-dev-experts




FLUTTERGUIDE
FlutterGuide is an easy learning guide platform to help you build apps for any screen. The leading Dart/Flutter guides for getting started and become pro.
Guides
  • Getting started with Dart 22
  • Private Documentation 1
Categories
  • Dart
  • iOS
  • API
  • Bloc
  • Firebase
  • Flutter
  • Widgets
  • Mobile
  • State Management
  • Design
  • Animations
  • Flutter Designs
About
About Us
Guides
Blogs
FAQs
Contact
  • Privacy Policy
  • Terms & Conditions
  • Copyright 2022 Fawesome Apps. All Rights Reserved