Dart| Flutter How to: Create an Empty Map

This tutorial shows multiple ways to create an empty Map in Dart or Flutter

How to create an empty Map in Dart and Flutter

An empty Map is a Map data structure that lacks a key and value pair of elements, and a Map has a length of zero.

In Flutter, an API or service method may return an empty Map instead of null to improve null safety handling.

How to create an Empty Map in Flutter or dart?

An empty Map can be created in multiple ways.

  • using type and brackets

You can create a variable for any class using type and brackets() in dart

  Map map = Map();
  print(map.length); //0
  print(map.runtimeType); //JsLinkedHashMap<dynamic, dynamic>

or you can use the new keyword to create a variable for the map

  Map map = new Map();
  print(map.length); //0
  print(map.runtimeType); //JsLinkedHashMap<dynamic, dynamic>

The above codes create an empty map and return Map.length as zero(0)

It is an empty map without type safety and the default type is Map<dynamic, dynamic>.

How do you create a typed Map?

  Map<String,String> map = Map<String,String>();
  print(map.length); //0
  print(map.runtimeType); //JsLinkedHashMap<String, String>
  • using empty literal syntax

Create a variable and assign the with empty literal using [] syntax.

  Map map = {};
  print(map.length); //0
  print(map.runtimeType); //JsLinkedHashMap<dynamic, dynamic>

To create a typed version of the empty literal syntax This creates an empty array typed map without elements.

  Map<int,String> map = <int,String>{};
  print(map.length); //0
  print(map.runtimeType); //JsLinkedHashMap<int, String>

or you can also use empty literal without type, but the variable type is typed.

  Map<int,String> map = {};
  print(map.length); //0
  print(map.runtimeType); //JsLinkedHashMap<dynamic, dynamic>

How to Create an Empty Map of objects in a flutter

The fourth way, Create an Empty Object Map

Here is an example code for multiple ways to create a Map of objects create a class of employees and create a Map empty object using var emps = <int, Employee>[];

class Employee {
  final int id;
  final String name;
  final int salary;
  final DateTime joinDate;

  Employee(this.id, this.name, this.salary, this.joinDate);

void main() {
  Map<int, Employee> emps1 = new Map<int, Employee>();
  print(emps1.length); //0
  print(emps1.runtimeType); //JsLinkedHashMap<dynamic, dynamic>

  Map<int, Employee> emps2 = <int, Employee>{};
  print(emps2.length); //0
  print(emps2.runtimeType); //JsLinkedHashMap<int, Employee>

  Map<int, Employee> emps3 = {};
  print(emps3.length); //0
  print(emps3.runtimeType); //JsLinkedHashMap<int, Employee>


This tutorial talks about multiple ways to create an empty Map with int, string, and objects.

Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.

Related posts

Dart| Flutter: Difference between dynamic vs object with examples

How to convert Integer to String or String to Integer in Dart or Flutter | Dart By Example

Dart| Flutter How to: Read pubspec.yaml attributes (version) with examples

How to Create and build a Singleton Class Dart or Flutter example| Dart By Example

What is the difference between var and dynamic and final in dart?| Flutter By Example

How to: Check if Phone number is valid or not in Dart| Flutter By Example

Dart| Flutter How to read an image from a disk and resize a file