You are currently viewing Dart Enums

Dart Enums

Dart, the open-source, general-purpose programming language developed by Google, is renowned for its simplicity and efficiency. One of the powerful features Dart offers is Enums, short for enumerations. Enums provide a concise way to represent a fixed set of values, making code more readable and maintainable. In this article, we’ll explore Dart Enums, and provide code examples to solidify your understanding.

What are Enums?

Enums, or enumerations, are a data type that consists of a set of named values, often referred to as members or elements. These members are distinct, and each represents a unique, constant value. In Dart, Enums are declared using the enum keyword, making it easy to define a collection of related constants.

Declaring Enums

Let’s start by understanding how to declare an enum in Dart. The syntax is simple and concise, making it easy to define a set of related constants. Consider the following example:

enum Days {
  Monday,
  Tuesday,
  Wednesday,
  Thursday,
  Friday,
  Saturday,
  Sunday
}

In this example, we’ve declared an enum named Days with seven enumerators representing the days of the week. By default, the first enumerator, Monday, is assigned the index 0, and subsequent enumerators are assigned consecutive integers.

Using Enums

Once an enum is defined, you can use its values in your code. For instance, you can assign enum values to variables or use them directly in switch statements.

void main() {

  var today = Days.Wednesday;

  switch (today) {
  
    case Days.Monday:
      print("It's Monday. Start of the week!");
      break;
	  
    case Days.Wednesday:
      print("It's Wednesday. Halfway through!");
      break;
	  
    case Days.Friday:
      print("It's Friday. Almost there!");
      break;
	  
    default:
      print("It's another day. Keep going!");
	  
  }
}

Enums provide a clear and self-documenting way to represent a set of related values. In the example above, the use of Days.Wednesday is more expressive than using arbitrary integers or strings. A switch statement is used to provide different messages based on the day.

Enum Values and Indexing

Each value in an enum has an index associated with it, starting from zero. You can access this index using the index property.

void main() {

  print(Days.Monday.index); // Output: 0
  print(Days.Friday.index); // Output: 4

}

This indexing can be useful in scenarios where you need to work with the numeric representation of enum values.

Iterating through Enums

Dart Enums can be iterated through using the values property, which returns a list of all enum constants in the order they are declared. This is useful when you need to perform operations on all possible values of an enum. Consider the following example:

void main() {

  for (var day in Days.values) {
    print(day);
  }
  
}

In this example, we use a for loop to iterate over the values of the Days enum. This can be handy when you need to perform operations on each enum member.

Enums for Type Safety

Enums contribute significantly to type safety in Dart. By restricting variable values to a predefined set, Enums help catch errors at compile-time, preventing unexpected values from creeping into your code. This is particularly beneficial in situations where certain values are expected and any deviation could lead to bugs.

Consider the following example where an enum is used to represent different shapes:

enum Shape {
  CIRCLE,
  SQUARE,
  TRIANGLE
}

class ShapePainter {

  void drawShape(Shape shape) {
  
    switch (shape) {
	
      case Shape.CIRCLE:
        print('Drawing a circle.');
        break;
		
      case Shape.SQUARE:
        print('Drawing a square.');
        break;
		
      case Shape.TRIANGLE:
        print('Drawing a triangle.');
        break;
		
    }
	
  }
  
}

void main() {

  ShapePainter painter = ShapePainter();
  
  // Correct usage
  painter.drawShape(Shape.CIRCLE);
  
  // Uncommenting the line below results in a compile-time error
  // painter.drawShape(Shape.RECTANGLE);
  
}

In this example, the ShapePainter class uses the Shape enum to define the type of shape to draw. Attempting to pass an invalid shape to the drawShape method results in a compile-time error, ensuring that only valid shapes are used.

Associating Enums with Data

In Dart, enums can have fields, constructors, and methods, similar to classes. However, enums only allow final instance variables and const constructors. Enums cannot be instantiated; consequently, all instances of the enum must be declared at the beginning of the declaration, with at least one instance required.

enum Days {

  Monday(description: "The day is Monday"),
  Tuesday(description: "The day is Tuesday"),
  Wednesday(description: "The day is Wednesday"),
  Thursday(description: "The day is Thursday"),
  Friday(description: "The day is Friday"),
  Saturday(description: "The day is Saturday"),
  Sunday(description: "The day is Sunday");

  const Days({required this.description});

  final String description;

  // Can have methods too
  void printDay() {
    print("The day description is: ${this.description}");
  }

  bool isFriday() => this == Days.Friday;

}

void main() {

  var today = Days.Monday;

  print(today.description); // Output: "The day is Monday"

  print(today.isFriday()); // Output: false

  today.printDay(); // Output: The day description is: The day is Monday

}

In this example, we declared a Days enum with descriptions for each day and provided methods such as isFriday to check whether a day is Friday. Additionally, we included the printDay() method and the description field, which prints the day description and holds the day description, respectively. Note that all instances of the enum are declared at the beginning of the enum because outside that context, enums cannot be instantiated, as they are a fixed set of values.

Conclusion

Dart Enums provide a powerful and expressive way to represent a fixed set of values within a variable. From simple enumerations to advanced use cases, enums in Dart offer a clean and readable solution. Whether you are working on a Flutter application or a Dart-based backend, understanding and using the features of Dart Enums can significantly improve the clarity and maintainability of your code.

Leave a Reply