Enumerated types

Enumerated types, often called enumerations or enums, are a special kind of class used to represent a fixed number of constant values.

Declaring simple enums

To declare a simple enumerated type, use the enum keyword and list the values you want to be enumerated:

enum Color { red, green, blue }

Declaring enhanced enums

Dart also allows enum declarations to declare classes with fields, methods, and const constructors which are limited to a fixed number of known constant instances.

To declare an enhanced enum, follow a syntax similar to normal classes, but with a few extra requirements:

  • Instance variables must be final, including those added by mixins.
  • All generative constructors must be constant.
  • Factory constructors can only return one of the fixed, known enum instances.
  • No other class can be extended as Enum is automatically extended.
  • There cannot be overrides for index, hashCode, the equality operator ==.
  • A member named values cannot be declared in an enum, as it would conflict with the automatically generated static values getter.
  • All instances of the enum must be declared in the beginning of the declaration, and there must be at least one instance declared.

Here is an example that declares an enhanced enum with multiple instances, instance variables, a getter, and an implemented interface:

enum Vehicle implements Comparable<Vehicle> {
  car(tires: 4, passengers: 5, carbonPerKilometer: 400),
  bus(tires: 6, passengers: 50, carbonPerKilometer: 800),
  bicycle(tires: 2, passengers: 1, carbonPerKilometer: 0);

  const Vehicle({
    required this.passengers,
    required this.carbonPerKilometer,

  final int tires;
  final int passengers;
  final int carbonPerKilometer;

  int get carbonFootprint => (carbonPerKilometer / passengers).round();

  int compareTo(Vehicle other) => carbonFootprint - other.carbonFootprint;

Using enums

Access the enumerated values like any other static variable:

final favoriteColor =;
if (favoriteColor == {
  print('Your favorite color is blue!');

Each value in an enum has an index getter, which returns the zero-based position of the value in the enum declaration. For example, the first value has index 0, and the second value has index 1.

assert( == 0);
assert( == 1);
assert( == 2);

To get a list of all the enumerated values, use the enum’s values constant.

List<Color> colors = Color.values;
assert(colors[2] ==;

You can use enums in switch statements, and you’ll get a warning if you don’t handle all of the enum’s values:

var aColor =;

switch (aColor) {
    print('Red as roses!');
    print('Green as grass!');
  default: // Without this, you see a WARNING.
    print(aColor); // ''

If you need to access the name of an enumerated value, such as 'blue' from, use the .name property:

print(; // 'blue'