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 staticvalues
getter. - All instances of the enum must be declared in the beginning of the declaration, and there must be at least one instance declared.
Instance methods in an enhanced enum can use this
to
reference the current enum value.
Here is an example that declares an enhanced enum with multiple instances, instance variables, getters, 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.tires,
required this.passengers,
required this.carbonPerKilometer,
});
final int tires;
final int passengers;
final int carbonPerKilometer;
int get carbonFootprint => (carbonPerKilometer / passengers).round();
bool get isTwoWheeled => this == Vehicle.bicycle;
@override
int compareTo(Vehicle other) => carbonFootprint - other.carbonFootprint;
}
Using enums
Access the enumerated values like any other static variable:
final favoriteColor = Color.blue;
if (favoriteColor == Color.blue) {
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(Color.red.index == 0);
assert(Color.green.index == 1);
assert(Color.blue.index == 2);
To get a list of all the enumerated values,
use the enum’s values
constant.
List<Color> colors = Color.values;
assert(colors[2] == Color.blue);
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 = Color.blue;
switch (aColor) {
case Color.red:
print('Red as roses!');
case Color.green:
print('Green as grass!');
default: // Without this, you see a WARNING.
print(aColor); // 'Color.blue'
}
If you need to access the name of an enumerated value,
such as 'blue'
from Color.blue
,
use the .name
property:
print(Color.blue.name); // 'blue'
You can access a member of an enum value like you would on a normal object:
print(Vehicle.car.carbonFootprint);