Dart List isEmpty
Syntax & Examples
Syntax of List.isEmpty
The syntax of List.isEmpty property is:
bool isEmpty This isEmpty property of List whether this collection has no elements.
Return Type
List.isEmpty returns value of type bool.
✐ Examples
1 Check if a list of numbers is empty
In this example,
- We create a list named
numberswith elements[1, 2, 3]. - We use the
isEmptyproperty to check if the list is empty. - The result is stored in
isEmptyNumbers. - We print whether the list is empty to standard output.
Dart Program
void main() {
List<int> numbers = [1, 2, 3];
bool isEmptyNumbers = numbers.isEmpty;
print('Is numbers empty? $isEmptyNumbers'); // Output: Is numbers empty? false
}Output
Is numbers empty? false
2 Check if a list of characters is empty
In this example,
- We create a list named
characterswith elements['a', 'b', 'c']. - We use the
isEmptyproperty to check if the list is empty. - The result is stored in
isEmptyCharacters. - We print whether the list is empty to standard output.
Dart Program
void main() {
List<String> characters = ['a', 'b', 'c'];
bool isEmptyCharacters = characters.isEmpty;
print('Is characters empty? $isEmptyCharacters'); // Output: Is characters empty? false
}Output
Is characters empty? false
3 Check if a list of strings is empty
In this example,
- We create an empty list named
strings. - We use the
isEmptyproperty to check if the list is empty. - The result is stored in
isEmptyStrings. - We print whether the list is empty to standard output.
Dart Program
void main() {
List<String> strings = [];
bool isEmptyStrings = strings.isEmpty;
print('Is strings empty? $isEmptyStrings'); // Output: Is strings empty? true
}Output
Is strings empty? true
Summary
In this Dart tutorial, we learned about isEmpty property of List: the syntax and few working examples with output and detailed explanation for each example.