Difference between List and Map in flutter

Dart Programming - Map

Advertisements

Previous Page
Next Page

The Map object is a simple key/value pair. Keys and values in a map may be of any type. A Map is a dynamic collection. In other words, Maps can grow and shrink at runtime.

Maps can be declared in two ways

  • Using Map Literals
  • Using a Map constructor

Declaring a Map using Map Literals

To declare a map using map literals, you need to enclose the key-value pairs within a pair of curly brackets "{ }".

Here is its syntax

var identifier = { key1:value1, key2:value2 [,..,key_n:value_n] }

Declaring a Map using a Map Constructor

To declare a Map using a Map constructor, we have two steps. First, declare the map and second, initialize the map.

The syntax to declare a map is as follows

var identifier = new Map[]

Now, use the following syntax to initialize the map

map_name[key] = value

Example: Map Literal

Live Demo
void main[] { var details = {'Usrname':'tom','Password':'pass@123'}; print[details]; }

It will produce the following output

{Usrname: tom, Password: pass@123}

Example: Adding Values to Map Literals at Runtime

Live Demo
void main[] { var details = {'Usrname':'tom','Password':'pass@123'}; details['Uid'] = 'U1oo1'; print[details]; }

It will produce the following output

{Usrname: tom, Password: pass@123, Uid: U1oo1}

Example: Map Constructor

Live Demo
void main[] { var details = new Map[]; details['Usrname'] = 'admin'; details['Password'] = 'admin@123'; print[details]; }

It will produce the following output

{Usrname: admin, Password: admin@123}

Note A map value can be any object including NULL.

Map Properties

The Map class in the dart:core package defines the following properties

Sr.NoProperty & Description
1Keys

Returns an iterable object representing keys

2Values

Returns an iterable object representing values

3Length

Returns the size of the Map

4isEmpty

Returns true if the Map is an empty Map

5isNotEmpty

Returns true if the Map is an empty Map

Map - Functions

Following are the commonly used functions for manipulating Maps in Dart.

Sr.NoFunction Name & Description
1addAll[]

Adds all key-value pairs of other to this map.

2clear[]

Removes all pairs from the map.

3remove[]

Removes key and its associated value, if present, from the map.

4forEach[]

Applies f to each key-value pair of the map.

Useful Video Courses

Video

Dart tutorial for Beginners

44 Lectures 4.5 hours

Sriyank Siddhartha

More Detail
Video

Flutter Tutorial for Beginners with Dart

34 Lectures 4 hours

Sriyank Siddhartha

More Detail
Video

Dart Masterclass Programming: iOS/Android Bible

69 Lectures 4 hours

Frahaan Hussain

More Detail
Video

Flutter & Dart Development For Building iOS and Android Apps

117 Lectures 10 hours

Frahaan Hussain

More Detail
Video

COMPLETE Google DART Programming- BOOTCAMP

22 Lectures 1.5 hours

Pranjal Srivastava

More Detail
Video

AWS Mobile with Google Dart

34 Lectures 3 hours

Pranjal Srivastava

More Detail
Previous Page Print Page
Next Page
Advertisements

Video liên quan

Chủ Đề