Sub topic 10
Enums and Extension Methods
Assume you know Dart variables, functions, classes, and basic control flow. This chapter focuses on two features that help you represent fixed choices and add reusable behavior without changing a class's source code.
Enums represent fixed choices
An enum is a type with a limited, named set of values. Use one when a value must be one of several known options.
enum Status {
idle,
loading,
success,
failure,
}
String describe(Status status) {
switch (status) {
case Status.idle:
return 'Not started';
case Status.loading:
return 'In progress';
case Status.success:
return 'Completed';
case Status.failure:
return 'Failed';
}
}
void main() {
print(describe(Status.success));
}Each enum value is an object. Status.success is different from the string 'success'. This prevents spelling mistakes and makes invalid states harder to represent.
Every enum has useful members:
Status.valuesreturns the values in declaration order.status.namereturns the declared name as a string.status.indexreturns its zero-based position.
Do not usually store index as permanent data. Reordering enum values changes those numbers.
Enhanced enums store data and behavior
An enhanced enum can have fields, constructors, and methods. Its fields must be final, and its constructors must be constant.
enum Planet {
earth(9.8),
mars(3.7);
const Planet(this.gravity);
final double gravity;
double weightOn(double earthWeight) {
return earthWeight * gravity / 9.8;
}
}
void main() {
print(Planet.mars.gravity);
print(Planet.mars.weightOn(60));
}The constructor argument gives each value different data. Planet.mars.weightOn(60) calls behavior defined by the enum itself.
A switch over an enum should handle every value. Dart can then check that the switch is exhaustive.
Extension methods add focused helpers
An extension adds methods, getters, or setters to an existing type without editing that type. The new member is available using normal dot notation.
extension StringShout on String {
String get shout => '${toUpperCase()}!';
}
extension IntFormatting on int {
String get evenOdd => isEven ? 'even' : 'odd';
}
void main() {
print('hello'.shout);
print(7.evenOdd);
}An extension does not create a subclass and cannot add stored fields. It also cannot access private state inside another library. The receiver's original type stays unchanged.
Dart chooses extension members using the expression's static type, which is the type known by the compiler. If a class already has a member with the same name, the class member wins.
| Feature | Enum | Extension |
|---|---|---|
| Main purpose | Represent fixed values | Add helper behavior |
| Stores per-value data | Yes, with enhanced enums | No stored fields |
| Creates a new type | Yes | No |
| Works through dot notation | Yes | Yes |
Choosing between them
Use an enum when the set of values is meaningful and limited. Use an extension when behavior belongs naturally beside a type but should remain separate from that type's definition.
Exercise
What does Planet.mars.gravity access, and why is shout an extension getter rather than an enum member?
Show answer
Planet.mars.gravity accesses data stored by an enhanced enum value. shout is an extension getter because it adds behavior to every String without changing the String class.