Dart Enums
Dart Enums
An enum (short for "enumeration") is a type with a fixed list of values.
Use an enum when a value can only be one of a few known choices, like the days of the week, the sizes of a T-shirt or the levels in a game.
You create an enum with the enum keyword, and separate the values with commas:
Example
enum Level { low, medium, high }
void main() {
var myLevel = Level.medium;
print(myLevel);
}
Try it Yourself »
Result:
Level.medium
You get a value with the enum name, a dot, and the value name, like Level.medium.
A variable of type Level can only hold one of the three values.
name and index
Every enum value has a name and an index.
nameis the name of the value as a stringindexis the position of the value in the enum, starting at 0
Example
enum Size { small, medium, large }
void main() {
var shirt = Size.large;
print(shirt.name);
print(shirt.index);
}
Try it Yourself »
Result:
large
2
Loop Through an Enum
The values property is a list of all values in the enum, in the order they were written:
Example
enum Day { monday, tuesday, wednesday, thursday, friday }
void main() {
print(Day.values.length);
for (var day in Day.values) {
print('${day.index}: ${day.name}');
}
}
Try it Yourself »
Result:
5
0: monday
1: tuesday
2: wednesday
3: thursday
4: friday
You can also compare enum values with ==:
Example
enum Day { monday, tuesday, wednesday, thursday, friday }
void main() {
var today = Day.friday;
if (today == Day.friday) {
print('It is almost the weekend!');
}
}
Try it Yourself »
Result:
It is almost the weekend!
Enums in a switch
Enums work very well with the switch statement:
Example
enum TrafficLight { red, yellow, green }
void main() {
var light = TrafficLight.yellow;
switch (light) {
case TrafficLight.red:
print('Stop');
case TrafficLight.yellow:
print('Get ready');
case TrafficLight.green:
print('Go');
}
}
Try it Yourself »
Result:
Get ready
Dart checks that the switch handles every value of the enum.
If you forget a value, you get an error:
Example
enum TrafficLight { red, yellow, green }
void main() {
var light = TrafficLight.green;
switch (light) {
case TrafficLight.red:
print('Stop');
case TrafficLight.yellow:
print('Get ready');
}
}
Try it Yourself »
Result:
main.dart:6:11: Error: The type 'TrafficLight' is not exhaustively matched by the switch cases since it doesn't match 'TrafficLight.green'.
- 'TrafficLight' is from 'main.dart'.
Try adding a default case or cases that match 'TrafficLight.green'.
switch (light) {
^
This helps you when you add a new value to an enum later, because Dart shows you every switch that must be updated.
Enhanced Enums
An enum can also have fields, a constructor and methods.
This is called an enhanced enum.
- Each value calls the constructor with its own data, like
mercury(0.38) - The fields must be
final - The constructor is usually written as
const - After the last value, write a semicolon
;instead of a comma
Example
enum Planet {
mercury(0.38),
earth(1.0),
mars(0.38),
jupiter(2.53);
final double gravity;
const Planet(this.gravity);
double weightOn(double earthWeight) => earthWeight * gravity;
}
void main() {
print(Planet.jupiter.gravity);
print(Planet.mars.weightOn(50));
}
Try it Yourself »
Result:
2.53
19.0
An enhanced enum can have several fields and getters too:
Example
enum Coffee {
espresso('Espresso', 2.5),
latte('Latte', 3.8),
cappuccino('Cappuccino', 3.5);
final String label;
final double price;
const Coffee(this.label, this.price);
bool get isCheap => price < 3;
}
void main() {
for (var coffee in Coffee.values) {
print('${coffee.label}: \$${coffee.price}, cheap: ${coffee.isCheap}');
}
}
Try it Yourself »
Result:
Espresso: $2.5, cheap: true
Latte: $3.8, cheap: false
Cappuccino: $3.5, cheap: false
Note: You cannot create new enum values with the constructor.
The only values are the ones listed at the top of the enum.