Enums topic

To use an enum with dart_mappable add the @MappableEnum annotation like this:

@MappableEnum()
enum MyEnum { ... }

By default, each enum value will be serialized to its name as a String. You can also use the caseStyle configuration option to change the generated values.

Custom Values

When using the @MappableEnum() annotation you can set the mode property to one of two ValuesModes for the encoding of enum values:

  • ValuesMode.named (default) will encode all values to their respective names as Strings, after applying the optional caseStyle.
  • ValuesMode.indexed will encode all values to their respective index as ints.

You can also specify a custom encoded value for each enum value using the @MappableValue(myCustomValue) annotation. It is possible to mix the encoding mode with custom values, and use values of different types.

@MappableEnum(mode: ValuesMode.indexed)
enum Status {
  zero,                         // encodes to 0
  @MappableValue(200) success,  // encodes to 200
  @MappableValue('error') error // encodes to 'error'
}

Next: Configuration

Classes

MappableEnum Enums
Used to annotate an enum in order to generate mapping code
MappableValue Enums
Used to annotate an enum value in order to define a custom encoded value.

Enums

ValuesMode Enums
The mode used for encoding the enum values. Can be ValuesMode.named to map each enum value to its name as String or ValuesMode.indexed to map each enum value to its index as int.