Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart Enums


Share

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.

  • name is the name of the value as a string
  • index is 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.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.