Provides Dart Build System builders for handling Enum.

Setup

To configure your project for the latest released version of mobkit_generator, see the example.

Example

Given a library example.dart with an Person enum annotated with EnumSerializable:

import 'package:mobkit_generator/annotations.dart';

part 'example.g.dart'

@EnumSerializable(String)
enum PersonStr {
  @EnumValue('John')
  name,
  @EnumValue('66')
  number,
}

@EnumSerializable(int)
enum PersonInt {
  @EnumValue(1)
  name,
  @EnumValue('66')
  number,
}

Building creates the corresponding part example.g.dart:

part of 'example.dart';


const Map<PersonStr, String> personStrEnumMap = {
  PersonStr.name: 'John',
  PersonStr.number: '66',
};

extension PersonStrExtension on PersonStr {
  String toValue() {
    return personStrEnumMap[this]!;
  }
}


const Map<PersonInt, int> PersonIntEnumMap = {
  PersonInt.name: 1,
  PersonInt.number: 66,
};

extension PersonIntExtension on PersonInt {
  int toValue() {
    return personIntEnumMap[this]!;
  }
}

Running the code generator

Once you have added the annotations to your code you then need to run the code generator to generate the missing .g.dart generated dart files.

With a Dart package, run dart run build_runner build in the package directory.

With a Flutter package, run flutter pub run build_runner build in your package directory.

Annotation values

The only annotation required to use this package is EnumValues. When applied to a enum (in a correctly configured package), toValue code will be generated when you build.

Custom Annotations

Annotation can be added on Enum with JsonValue and Enum Value.

import 'package:mobkit_generator/annotations.dart';

part 'example.g.dart'

@EnumSerializable()
enum Person {
  @EnumValue('John')
  name,
  @JsonValue('66')
  number,
}