Using Enums in Variables
Part of the Logic & Flow section of Coddy's Dart journey — lesson 60 of 65.
Now that you know how to define enums, let's learn how to actually use them in your programs. Once you've created an enum, you can declare variables of that enum type and assign specific values to them.
To declare a variable of an enum type, you specify the enum name followed by the variable name, just like with any other data type:
Weather todayWeather = Weather.sunny;Notice how you access enum values using dot notation: Weather.sunny. This tells Dart that you want the sunny value from the Weather enum.
You can change the variable's value to any other valid enum value:
todayWeather = Weather.rainy;
todayWeather = Weather.cloudy;The beauty of using enum variables is that Dart ensures you can only assign valid enum values. You cannot accidentally assign "stormy" or any other invalid value to a Weather variable, which prevents common programming errors and makes your code more reliable.
Challenge
EasyCreate a program that manages a simple task tracking system using enums and variables. Your task is to:
- Define an enum called
TaskStatuswith the following values:todo,inProgress,testing,completed, andcancelled - Create a variable called
task1Statusof typeTaskStatusand assign it the valueTaskStatus.todo - Create a variable called
task2Statusof typeTaskStatusand assign it the valueTaskStatus.inProgress - Create a variable called
task3Statusof typeTaskStatusand assign it the valueTaskStatus.completed - Print the initial status of all tasks with the exact format:
Task 1 status: TaskStatus.todo - Print the initial status of task 2 with the exact format:
Task 2 status: TaskStatus.inProgress - Print the initial status of task 3 with the exact format:
Task 3 status: TaskStatus.completed - Update
task1StatustoTaskStatus.inProgress - Update
task2StatustoTaskStatus.testing - Print the updated status of task 1 with the exact format:
Updated Task 1 status: TaskStatus.inProgress - Print the updated status of task 2 with the exact format:
Updated Task 2 status: TaskStatus.testing - Print the final status of task 3 with the exact format:
Final Task 3 status: TaskStatus.completed
Your program should demonstrate proper enum definition using the enum keyword, variable declaration with enum types, accessing enum values using dot notation, and updating enum variables. Make sure to follow Dart naming conventions where the enum name starts with a capital letter and enum values are written in lowercase.
Cheat sheet
To declare a variable of an enum type, specify the enum name followed by the variable name:
Weather todayWeather = Weather.sunny;Access enum values using dot notation with the format EnumName.value:
todayWeather = Weather.rainy;
todayWeather = Weather.cloudy;Dart ensures you can only assign valid enum values to enum variables, preventing invalid assignments and making code more reliable.
Try it yourself
import 'dart:io';
// TODO: Define your TaskStatus enum here
void main() {
// TODO: Write your code here
// 1. Create task1Status variable and assign TaskStatus.todo
// 2. Create task2Status variable and assign TaskStatus.inProgress
// 3. Create task3Status variable and assign TaskStatus.completed
// 4. Print initial status of all tasks
// 5. Update task statuses as required
// 6. Print updated and final statuses
}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 List6Basic Error Handling
What are Exceptions?The 'try-catch' BlockCatching Exceptions with 'on'The 'finally' BlockThrowing an ExceptionRecap - Safe Division9Enumerations (Enums)
What are Enums?Defining a Simple EnumUsing Enums in VariablesEnums in 'switch' StatementsRecap - Traffic Light