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:
- name type using
UpperCamelCase
https://dart.dev/effective-dart/style#do-name-types-using-uppercamelcase - name value using
lowerCamelCase
https://dart.dev/effective-dart/style#prefer-using-lowercamelcase-for-constant-names
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?