{

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 a 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 is, 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>
}

Conclusion

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

THE BEST NEWSLETTER ANYWHERE
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
Subscribe
You'll get a notification every time a post gets published here.