List State: isEmpty & isNotEmp
Part of the Logic & Flow section of Coddy's Dart journey — lesson 2 of 65.
Before working with list elements, it's essential to know whether your list actually contains any items. Dart provides two simple properties to check the state of a list: isEmpty and isNotEmpty.
The isEmpty property returns true if the list has no elements, and false if it contains at least one element. The isNotEmpty property works in the opposite way - it returns true when the list has elements and false when it's empty.
List<String> emptyCart = [];
List<String> shoppingCart = ['apples', 'bread', 'milk'];
print(emptyCart.isEmpty); // true
print(emptyCart.isNotEmpty); // false
print(shoppingCart.isEmpty); // false
print(shoppingCart.isNotEmpty); // trueThese properties are particularly useful for preventing errors when trying to access list elements. Checking if a list is empty before using properties like .first or .last helps you write safer, more reliable code.
Challenge
EasyCreate a program that manages a task queue system by checking if task lists are empty or contain items. Your program should:
- Read a string input representing the queue name
- Read multiple string inputs representing tasks (the input will end when you receive an empty string)
- Use the
isEmptyproperty to check if the task list is empty - Use the
isNotEmptyproperty to check if the task list contains tasks - Print the queue status in the exact format shown below
For example, if the queue name is "Daily Tasks" and the tasks are "Review emails", "Attend meeting", "Write report", your program should output:
Queue: Daily Tasks
Has tasks: true
Is empty: false
Task count: 3
Status: Ready for processingIf the queue name is "Weekend Tasks" and no tasks are provided (empty input), your program should output:
Queue: Weekend Tasks
Has tasks: false
Is empty: true
Task count: 0
Status: No tasks to processYour program must use both isEmpty and isNotEmpty properties to determine the queue status. The status message should be "Ready for processing" when tasks exist and "No tasks to process" when the queue is empty.
Cheat sheet
Use isEmpty and isNotEmpty properties to check if a list contains elements:
List<String> emptyCart = [];
List<String> shoppingCart = ['apples', 'bread', 'milk'];
print(emptyCart.isEmpty); // true
print(emptyCart.isNotEmpty); // false
print(shoppingCart.isEmpty); // false
print(shoppingCart.isNotEmpty); // trueThese properties help prevent errors when accessing list elements and make code safer and more reliable.
Try it yourself
import 'dart:io';
void main() {
// Read queue name
String? queueName = stdin.readLineSync();
// Read tasks until empty string is received
List<String> tasks = [];
String? task;
while ((task = stdin.readLineSync()) != null && task!.isNotEmpty) {
tasks.add(task);
}
// TODO: Write your code below
// Use tasks.isEmpty and tasks.isNotEmpty properties to check the queue status
// Determine the appropriate status message based on whether tasks exist
// Output the results
print('Queue: $queueName');
// Print has tasks, is empty, task count, and status
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced List Manipulation
List Properties: first & lastList State: isEmpty & isNotEmpReversing a ListAdding to a List: insertList Removal: removeWhereFinding in a List: indexOfSorting a ListShuffling a ListRecap - List Organizer4Advanced Map Manipulation
Iterating Over a MapChecking for Keys and ValuesMap Properties: keys & valuesConditional Add: putIfAbsentRemoving Entries from a MapNested MapsRecap - Inventory Update2Functional List Operations
Transforming with 'map'Filtering with 'where'Using '.toList()'Checking Conditions with 'any'Conditions with 'every'Finding with 'firstWhere'Recap - Data Filtering5Project: Shopping Cart Calc
Project SetupAdding Items to the Cart3Sets
What is a Set?Creating a SetAdding and Removing from SetsChecking for Elements in a SetConverting a List to a SetSet UnionSet IntersectionSet DifferenceRecap - Unique Guest List