Menu
Coddy logo textTech

Defining a Simple Enum

Part of the Logic & Flow section of Coddy's Dart journey — lesson 59 of 65.

Now that you understand what enums are, let's learn how to create them in Dart. The syntax is straightforward and follows a simple pattern.

To define an enum, you use the enum keyword followed by the name of your enum and a list of values inside curly braces:

enum Weather {
  sunny,
  cloudy,
  rainy
}

Each value in the enum is separated by a comma, and by convention, enum values are written in lowercase. The enum name itself should start with a capital letter, following Dart's naming conventions.

Once you've defined an enum, you can use it throughout your program. For example, you could create enums for user roles, game states, or priority levels:

enum Priority {
  low,
  medium,
  high
}

Creating an enum is that simple! You're essentially creating a new data type with a fixed set of possible values that will make your code more reliable and easier to understand.

challenge icon

Challenge

Easy

Create a program that defines an enum for different types of programming languages and demonstrates its usage. Your task is to:

  1. Define an enum called ProgrammingLanguage with the following values: dart, python, javascript, java, and cpp
  2. Create a variable called currentLanguage of type ProgrammingLanguage and assign it the value ProgrammingLanguage.dart
  3. Create another variable called nextLanguage of type ProgrammingLanguage and assign it the value ProgrammingLanguage.python
  4. Print the current language with the exact format: Currently learning: ProgrammingLanguage.dart
  5. Print the next language with the exact format: Next to learn: ProgrammingLanguage.python
  6. Change the currentLanguage to ProgrammingLanguage.javascript
  7. Print the updated current language with the exact format: Updated current language: ProgrammingLanguage.javascript

Your program should demonstrate the proper syntax for defining an enum using the enum keyword, declaring variables of enum type, and accessing enum values using dot notation. Make sure to follow Dart naming conventions where enum names start with a capital letter and enum values are written in lowercase.

Cheat sheet

To define an enum in Dart, use the enum keyword followed by the enum name and values in curly braces:

enum Weather {
  sunny,
  cloudy,
  rainy
}

Enum values are separated by commas and written in lowercase. Enum names should start with a capital letter following Dart naming conventions.

You can create variables of enum type and access values using dot notation:

enum Priority {
  low,
  medium,
  high
}

Priority taskPriority = Priority.high;

Try it yourself

// TODO: Define your ProgrammingLanguage enum here

void main() {
  // TODO: Write your code here
  // 1. Create currentLanguage variable and assign ProgrammingLanguage.dart
  // 2. Create nextLanguage variable and assign ProgrammingLanguage.python
  // 3. Print current language in the required format
  // 4. Print next language in the required format
  // 5. Update currentLanguage to ProgrammingLanguage.javascript
  // 6. Print updated current language in the required format
}
quiz iconTest yourself

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

All lessons in Logic & Flow