Search code snippets, questions, articles...

Check if list is empty in Flutter/Dart

The List.isEmpty can be used in dart or flutter to check whether a list is empty or not.
List myList = [];
  
if(myList.isEmpty) {
    print('The list is empty');
}
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

Output

The list is empty

In the above code example:

  1. We have defined a list(myList) that will be checked if it is empty or not.
  2. We are using isEmpty to check whether the given list is empty or not.
  3. Printing "The list is empty" if the above process returns true.

Using List.length to check if the list is empty or not

We can also use List.length in Dart to check if a list is empty. This will return 0 if the list is empty. We can understand it using the below code example.

List myList = [];
  
if(myList.length == 0) {
  print('The list is empty');
}

Output

The list is empty
Was this helpful?
0 Comments