Dart Map isNotEmpty
Syntax & Examples
Syntax of Map.isNotEmpty
The syntax of Map.isNotEmpty property is:
bool isNotEmpty
This isNotEmpty property of Map whether there is at least one key/value pair in the map.
Return Type
Map.isNotEmpty returns value of type bool
.
✐ Examples
1 Check if map is not empty
In this example,
- We create a map named
map1
with key/value pairs {'apple': 1, 'banana': 2, 'cherry': 3}. - We then check if the map is not empty using the
isNotEmpty
property. - We print the result to standard output.
Dart Program
void main() {
Map<String, int> map1 = {'apple': 1, 'banana': 2, 'cherry': 3};
bool notEmpty = map1.isNotEmpty;
print('Map is not empty: $notEmpty');
}
Output
Map is not empty: true
2 Check if empty map is not empty
In this example,
- We create an empty map named
map2
. - We then check if the map is not empty using the
isNotEmpty
property. - We print the result to standard output.
Dart Program
void main() {
Map<int, String> map2 = {};
bool notEmpty = map2.isNotEmpty;
print('Map is not empty: $notEmpty');
}
Output
Map is not empty: false
3 Check if map with single entry is not empty
In this example,
- We create a map named
map3
with a single key/value pair {'name': 'John'}. - We then check if the map is not empty using the
isNotEmpty
property. - We print the result to standard output.
Dart Program
void main() {
Map<String, String> map3 = {'name': 'John'};
bool notEmpty = map3.isNotEmpty;
print('Map is not empty: $notEmpty');
}
Output
Map is not empty: true
Summary
In this Dart tutorial, we learned about isNotEmpty property of Map: the syntax and few working examples with output and detailed explanation for each example.