Skip to main content

Dart Enum

Dart's enum is similar to Java's enum, support both name and fields like other classes.

Define Enum

Only have names:

enum Color { red, green, blue }

Enhanced enum with Fields:

TODO: example

Style

It is different from Java's enum in that:

enum Color { red, lightBlue }

btw: Dart used to use Java's SCREAMING_CAPS

Get all enum values

The values property contains a list of the enum's values in the order they're declared. You can also get a map using asNamMap().

Parse enum from string

Use values.byName

TODO: What happens when the string is invalid?

References