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