0Pricing
Dart Academy · Lesson

Enhanced Enums With Fields and Methods

Attach data and behavior to each value.

Enhanced Enums With Fields and Methods is a free Dart Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Enums Can Carry Data

Modern Dart lets each enum member hold its own data. These are enhanced enums, and they turn simple labels into rich values.

Adding a Field

Declare a final field on the enum, then pass a value for each member. Every member now carries that extra data.

enum Planet {
  earth(9.8), mars(3.7);
  final double gravity;
}

A Const Constructor Is Required

Enhanced enums need a const constructor to set their fields. It runs once for each member as the values are created.

const Planet(this.gravity);

Members Pass Constructor Args

The values in parentheses are constructor arguments. earth(9.8) means earth is built with gravity set to 9.8.

earth(9.8), mars(3.7);

Reading a Member Field

Access the data through the member, just like any property. Each member returns its own stored value.

print(Planet.earth.gravity); // 9.8

Multiple Fields

An enhanced enum can hold several fields at once. List them in the constructor and supply each value per member.

enum Coin { penny(1, "1c"), dime(10, "10c"); }

Adding a Method

Enhanced enums can define methods too. The method can use the member fields, giving each value real behavior.

double weightOf(double mass) => mass * gravity;

Calling an Enum Method

Call the method on any member like a normal object. It runs using that members own field values.

print(Planet.mars.weightOf(70)); // 259.0

Adding a Getter

A computed getter works as well, returning a value derived from the fields without storing anything new.

bool get isStrong => gravity > 5;

They Are Still Enums

Despite the extra power, enhanced enums keep values, index, and name. You lose nothing by adding fields and methods.

print(Planet.values.length); // 2

When to Enhance

Use an enhanced enum when each choice owns related data, like a color with a hex code. It keeps data and choice together.

Quick Check

What must an enhanced enum with fields always include?

Recap: Enhanced Enums

You learned enhanced enums hold fields and methods via a const constructor, so each member carries its own data and behavior. 🚀

Frequently asked questions

Is the “Enhanced Enums With Fields and Methods” lesson free?

Yes — the full text of “Enhanced Enums With Fields and Methods” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.

What will I learn in “Enhanced Enums With Fields and Methods”?

Attach data and behavior to each value. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Dart Academy?

No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Enhanced Enums With Fields and Methods” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Dart Academy lesson?

Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Declaring and Using Plain Enums
  2. Enum Values, index, and name
  3. Enhanced Enums With Fields and Methods
  4. switch Over Enums Exhaustively
← Back to Dart Academy