Dart – Check if List is Empty
Use the isEmpty property to check whether a Dart list contains no elements. It returns true when the list has a length of zero and false otherwise.
void main() {
final numbers = <int>[];
if (numbers.isEmpty) {
print('List is empty.');
}
}
Dart also provides isNotEmpty for the opposite condition. Comparing length with zero works, but isEmpty and isNotEmpty usually express the intent more clearly.
Ways to Check Whether a Dart List Is Empty
list.isEmptyreturnstruewhen the list has no elements.list.isNotEmptyreturnstruewhen the list contains at least one element.list.length == 0checks whether the number of elements is zero.
These checks inspect the number of elements in the list. A list containing an empty string, null, zero, or another value is still not empty because it contains an element.
Check if a Dart List Is Empty Using length
In the following example, myList contains no elements. The condition myList.length == 0 therefore evaluates to true.
Dart Program
void main() {
var myList = [];
if (myList.length == 0) {
print('List is empty.');
} else {
print('List is not empty.');
}
}
Output
List is empty.
This approach is valid, but myList.isEmpty is shorter and directly states what the condition is testing.
Check if a Dart List Is Empty Using isEmpty
The isEmpty property is the standard choice when code should run only for a list with no elements. The next program checks both an empty list and a list containing three integers.
Dart Program
void checkList(var myList){
//isEmpty returns true if list is emtpy
if(myList.isEmpty){
print("List "+myList.toString()+" is empty");
} else{
print("List "+myList.toString()+" is not empty");
}
}
void main(){
var list1 = [];
checkList(list1);
var list2 = [24, 56, 84];
checkList(list2);
}
Output
D:\tutorialkart\workspace\dart_tutorial>dart example.dart
List [] is empty
List [24, 56, 84] is not empty
Check if a Dart List Has Elements Using isNotEmpty
Use isNotEmpty when the main branch should handle a list that contains one or more elements. It is the logical opposite of isEmpty.
Dart Program
void checkList(var myList){
//isEmpty returns true if list is emtpy
if(myList.isNotEmpty){
print("List "+myList.toString()+" is not empty");
} else{
print("List "+myList.toString()+" is empty");
}
}
void main(){
var list1 = [];
checkList(list1);
var list2 = [24, 56, 84];
checkList(list2);
}
Output
D:\tutorialkart\workspace\dart_tutorial>dart example.dart
List [] is empty
List [24, 56, 84] is not empty
Check an Empty Nullable List in Null-Safe Dart
A nullable list such as List<int>? may be either null or a list object. Decide whether null should be treated as empty before writing the condition.
The expression items?.isEmpty ?? true treats both null and an empty list as empty.
void checkItems(List<int>? items) {
if (items?.isEmpty ?? true) {
print('List is null or empty.');
} else {
print('List contains ${items.length} element(s).');
}
}
void main() {
checkItems(null);
checkItems(<int>[]);
checkItems(<int>[10, 20]);
}
Output
List is null or empty.
List is null or empty.
List contains 2 element(s).
When null and an empty list have different meanings in the program, check them separately:
if (items == null) {
print('No list was provided.');
} else if (items.isEmpty) {
print('A list was provided, but it is empty.');
} else {
print('The list contains elements.');
}
Empty List Versus a List Containing Empty Values
The isEmpty property checks only whether the list has elements. It does not inspect whether those elements are blank, zero, false, or null.
void main() {
final emptyList = <String>[];
final listWithEmptyString = <String>[''];
print(emptyList.isEmpty);
print(listWithEmptyString.isEmpty);
print(listWithEmptyString.length);
}
Output
true
false
1
Choosing Between isEmpty, isNotEmpty, and length
- Use
isEmptyfor conditions that handle a list with zero elements. - Use
isNotEmptywhen processing should continue only when at least one element exists. - Use
lengthwhen the exact number of elements is also needed, not merely to express an emptiness check. - For a nullable list, handle
nullexplicitly or use a null-aware expression that matches the program’s intended behavior.
Common Questions About Empty Lists in Dart
What does List.isEmpty return in Dart?
List.isEmpty returns a Boolean value. It is true when the list contains zero elements and false when the list contains one or more elements.
Is isEmpty better than length == 0 in Dart?
Both conditions check the same list state, but isEmpty is generally clearer because it directly describes the intent of the condition.
Does a Dart list containing null count as empty?
No. A list such as <int?>[null] has one element, so its isEmpty property is false.
How do I check whether a nullable Dart list is null or empty?
Use list?.isEmpty ?? true when both states should be treated as empty. Use separate null and isEmpty checks when the two states require different handling.
Dart List Emptiness Review Checklist
- Confirm that the condition checks the list itself rather than an unrelated variable.
- Prefer
isEmptyorisNotEmptywhen only list emptiness matters. - Verify how nullable lists should behave before using a null-aware condition.
- Remember that a list containing blank or
nullvalues is not an empty list. - Test both an empty list and a non-empty list so that every branch is covered.
Summary of Dart List Empty Checks
In this Dart Tutorial, we learned how to check whether a list is empty using isEmpty, isNotEmpty, and length. For most direct checks, use isEmpty or isNotEmpty, and handle nullable lists according to whether null should be treated as empty.
TutorialKart.com