Menu
Coddy logo textTech

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); // true

These 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 icon

Challenge

Easy

Create a program that manages a task queue system by checking if task lists are empty or contain items. Your program should:

  1. Read a string input representing the queue name
  2. Read multiple string inputs representing tasks (the input will end when you receive an empty string)
  3. Use the isEmpty property to check if the task list is empty
  4. Use the isNotEmpty property to check if the task list contains tasks
  5. 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 processing

If 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 process

Your 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); // true

These 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
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow