Enum Values, index, and name
Iterate and inspect enum members.
Enum Values, index, and name is a free Dart Academy lesson on CoddyKit — lesson 2 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 Know Their Members
Every enum quietly tracks all of its members for you. Dart gives you built-in ways to list, count, and inspect them.
The values List
Each enum has a static values list holding every member in order. It is your gateway to looping over an enum.
enum Color { red, green, blue }
print(Color.values); // [Color.red, ...]Looping Over values
Because values is a real list, a for-in walks every member. Great for building menus or printing all options.
for (var c in Color.values) print(c);The index Property
Each member has an index: its zero-based position in the declaration. The first member is always index 0.
print(Color.green.index); // 1index Follows Declaration Order
The index reflects the order you wrote the members. Reorder the declaration and the indexes shift with it.
print(Color.blue.index); // 2The name Property
The name property gives a member as a plain string, like "green". Perfect for logging or showing a label to users.
print(Color.green.name); // greenname Is Just the Member
Unlike printing the whole value, name drops the type prefix. You get the bare identifier, nothing more.
print(Color.red); // Color.red
print(Color.red.name); // redMember by index
Pair the values list with an index to fetch a member by position. Handy when a number maps to a known choice.
var c = Color.values[2];
print(c); // Color.blueFinding a Member by Name
Use values.byName to turn a string back into a member. It throws if the name does not match any member.
var c = Color.values.byName("blue");Counting Members
Need how many choices exist? The length of the values list answers instantly, no manual counting required.
print(Color.values.length); // 3Putting It Together
With values, index, and name you can iterate, position, and label any enum. These three cover most everyday enum tasks.
Quick Check
What does the name property return for an enum member?
Recap: values, index, name
You met values to list members, index for position, and name for the string label. Together they let you inspect any enum. 👍
Frequently asked questions
Is the “Enum Values, index, and name” lesson free?
Yes — the full text of “Enum Values, index, and name” 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 “Enum Values, index, and name”?
Iterate and inspect enum members. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Enum Values, index, and name” 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
- Declaring and Using Plain Enums
- Enum Values, index, and name
- Enhanced Enums With Fields and Methods
- switch Over Enums Exhaustively