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,
- We create a map named
map1
containing key-value pairs. - We use the
isEmpty
property ofmap1
to check if it's empty. Sincemap1
is not empty,isEmpty
returnsfalse
. - 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,
- We create an empty map named
map2
. - We use the
isEmpty
property ofmap2
to check if it's empty. Sincemap2
is empty,isEmpty
returnstrue
. - 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,
- We create a map named
map3
containing key-value pairs. - We use the
isEmpty
property ofmap3
to check if it's empty. Sincemap3
is not empty,isEmpty
returnsfalse
. - 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.