Skip to main content
An enumerated type (enum) is a special class structure used to represent a fixed set of constant values. Enums automatically extend the abstract Enum class. They are implicitly final and cannot be subclassed, implemented, or mixed in. Each value defined in an enum is a compile-time constant instance of that enum class.

Basic Declaration

The simplest enum declaration defines the type name and a comma-separated list of values.
enum Status {
  pending,
  approved,
  rejected
}

Members and Properties

Enums possess both instance properties inherited from the Enum base class and static members generated by the compiler. Instance Properties (Inherited):
  • index: A zero-based integer representing the value’s ordinal position in the declaration.
  • name: A string containing the source identifier of the value.
Static Members (Generated):
  • values: A constant list containing all enum instances in declaration order. This list is a compile-time constant and is valid for use in const contexts.
void main() {
  // Instance properties
  print(Status.approved.index); // Output: 1
  print(Status.approved.name);  // Output: approved

  // Static generated constant list
  print(Status.values);         // Output: [Status.pending, Status.approved, Status.rejected]
  
  // Valid in const context
  const allStatuses = Status.values; 
}

Enhanced Enums

Dart enums function as classes and can declare fields, methods, and constructors. Syntax Requirements:
  1. Fields: All instance variables must be final.
  2. Constructors: Generative constructors must be const. Factory constructors can be non-const.
  3. Syntax: The list of enum values must be declared first and terminated with a semicolon (;) if additional members follow.
enum Planet {
  mercury(type: PlanetType.terrestrial),
  venus(type: PlanetType.terrestrial),
  earth(type: PlanetType.terrestrial),
  jupiter(type: PlanetType.gasGiant); // Semicolon required

  // Final instance field
  final PlanetType type;

  // Const generative constructor
  const Planet({required this.type});

  // Method definition
  bool get isHabitable => this == Planet.earth;
}

enum PlanetType { terrestrial, gasGiant, iceGiant }

Interfaces and Mixins

While enums cannot extend other classes (as they already extend Enum), they can implement interfaces and apply mixins.
mixin Serializer {
  String toJson() => toString();
}

abstract class Describable {
  String get description;
}

enum Role with Serializer implements Describable {
  admin('Administrator'),
  user('Standard User');

  final String label;
  const Role(this.label);

  @override
  String get description => '$name: $label';
}

Exhaustiveness Checking

Dart performs exhaustiveness checking when enums are used in switch statements or expressions. If the switch logic does not cover every defined enum value and lacks a default case, the compiler emits an error.
Status currentStatus = Status.pending;

// Compile-time error if a case is missing
String message = switch (currentStatus) {
  Status.pending => 'Please wait',
  Status.approved => 'Go ahead',
  Status.rejected => 'Stop',
};

Comparison

Enum values are canonical instances. Comparison is performed by identity, meaning two references to the same enum value are considered equal.
var a = Status.pending;
var b = Status.pending;

print(a == b);          // true
print(identical(a, b)); // true
Master Dart with Deep Grasping Methodology!Learn More