Dart Tutorial

Enums

Simple and Enhanced Enumerations in Dart

What is an Enum?

An Enumeration (Enum) is a special kind of class used to represent a fixed number of constant values.
dart
enum Status { pending, success, failed }

Enhanced Enums (Dart 2.17+)

Wait! Enums in modern Dart are extremely powerful. They can have fields, constructors, and methods, just like a regular class.
dart
enum Plan {
  free(0, 'Basic'),
  pro(10, 'Professional'),
  gold(50, 'Unlimited');

  final int price;
  final String label;

  const Plan(this.price, this.label);

  bool get isExpensive => price > 20;
}

Exhaustive Switch

When you use an enum in a switch statement, the compiler ensures that you have handled every possible value.
dart
switch (currentPlan) {
  case Plan.free:
    // ...
  case Plan.pro:
    // ...
  case Plan.gold:
    // ...
}

Summary

TermDescription
enumKeyword to define a set of constants
valuesPlan.values - List of all cases
indexThe numeric position of a case (starts at 0)
constEnum constructors must be constants
Best ForStates, types, and fixed categories
LogicPredictable and type-safe data
SafetyCompiler prevents using invalid enum values
ModernEnhanced Enums make data-centric logic much cleaner
StandardUse lowerCamelCase for case names in Dart
IdentityEnums are a special type of Class
SwitchUse switch expressions for concise enum handling