Dart program to check if a list is empty or not

Dart program to check if a list is empty or not:

In this post, we will learn how to check if a list is empty or not in dart. An empty list contains no elements. Dart provides a couple of different properties to check if a list is empty or not.

I am showing all these three properties and how to use them to check if a list is empty or not with examples. You can pick any one of them for your App.

Method 1: length:

length is of type integer. This property returns the length of a list. If the length is zero, we can say that it is empty.

For example:

main(List<String> args) {
  List<int> intList = new List();
  intList.add(1);

  List<int> secondList = new List();

  if (intList.length == 0) {
    print('intList is empty');
  }

  if (secondList.length == 0) {
    print('secondList is empty');
  }
}

This program will print:

secondList is empty

This is because we have added one element to intList but not to secondList. So, its length is 0.

Now, let’s check the below program :

main(List<String> args) {

  List<int> myList;


  if (myList.length == 0) {
    print('myList is empty');
  }
}

If you run it, it will throw one error :

NoSuchMethodError: The getter 'length' was called on null.
Receiver: null

This is because we haven’t initialized the list myList. Calling length on an uninitialized list will always throw one exception.

Method 2: isEmpty property:

isEmpty is a boolean property that returns one boolean value if no element is present in the collection.

We can use this property to check if a list is empty or not:

main(List<String> args) {

  List<int> myList = new List();


  if (myList.isEmpty) {
    print('myList is empty');
  }
}

It will print myList is empty.

Similar to the above example, make sure to check if the list is null or not before calling isEmpty. Else, it will throw one exception.

Method 3: isNotEmpty property:

isNotEmpty is similar to isEmpty. It returns true if there is at least one element in the collection. So, we can add one ! check :

main(List<String> args) {

  List<int> myList = new List();


  if (!myList.isNotEmpty) {
    print('myList is empty');
  }
}

It will print the same output.

Similar to the above example, make sure to check for null.

Example using null check:

It looks as like below if we add one null check before calling any of these properties:

main(List<String> args) {

  List<int> myList = new List();

  if (myList != null && !myList.isNotEmpty) {
    print('myList is empty');
  }

  if (myList != null && myList.isEmpty) {
    print('myList is empty');
  }

  if (myList != null && myList.length == 0) {
    print('myList is empty');
  }
}

It will not throw any exception even if myList is null.

Dart check if list is empty or not

You might also like:

Where is the color and why codevscolor ?

Long story short, I love paintings and I paint on weekends. We(me and my wife) have one Youtube channel. Below is a video that I did recently. If you love this please do subscribe to support us 😊