Dart Map isEmpty
Syntax & Examples

Syntax of Map.isEmpty

The syntax of Map.isEmpty property is:

 bool isEmpty 

This isEmpty property of Map whether there is no key/value pair in the map.

Return Type

Map.isEmpty returns value of type bool.



✐ Examples

1 Check if a non-empty map is empty

In this example,

  1. We create a map named map1 containing key-value pairs.
  2. We use the isEmpty property of map1 to check if it's empty. Since map1 is not empty, isEmpty returns false.
  3. We print the result to standard output.

Dart Program

void main() {
  var map1 = {1: 'one', 2: 'two', 3: 'three'};
  print('Is map1 empty? ${map1.isEmpty}');
}

Output

Is map1 empty? false

2 Check if an empty map is empty

In this example,

  1. We create an empty map named map2.
  2. We use the isEmpty property of map2 to check if it's empty. Since map2 is empty, isEmpty returns true.
  3. We print the result to standard output.

Dart Program

void main() {
  var map2 = {};
  print('Is map2 empty? ${map2.isEmpty}');
}

Output

Is map2 empty? true

3 Check if another non-empty map is empty

In this example,

  1. We create a map named map3 containing key-value pairs.
  2. We use the isEmpty property of map3 to check if it's empty. Since map3 is not empty, isEmpty returns false.
  3. We print the result to standard output.

Dart Program

void main() {
  var map3 = {'x': 'apple', 'y': 'banana', 'z': 'cherry'};
  print('Is map3 empty? ${map3.isEmpty}');
}

Output

Is map3 empty? false

Summary

In this Dart tutorial, we learned about isEmpty property of Map: the syntax and few working examples with output and detailed explanation for each example.