Dart Tutorials

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,

  1. We create a list named numbers with elements [1, 2, 3].
  2. We use the isNotEmpty property to check if the list is not empty.
  3. The result is stored in isNotEmptyNumbers.
  4. 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,

  1. We create a list named characters with elements ['a', 'b', 'c'].
  2. We use the isNotEmpty property to check if the list is not empty.
  3. The result is stored in isNotEmptyCharacters.
  4. 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,

  1. We create an empty list named strings.
  2. We use the isNotEmpty property to check if the list is not empty.
  3. The result is stored in isNotEmptyStrings.
  4. 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.