mobkit_enum_generator 1.0.0
Provides Dart Build System builders for handling Enum. You can get values and descriptions from your enums.
Provides Dart Build System builders for handling Enum.
Setup
To configure your project for the latest released version of mobkit_enum_generator, see the example. If you want to get annotation with Enums, you must set the Description of EnumSerializable annotation to true
flutter pub add -d mobkit_enum_generator
Example
Given a library example.dart with an Person enum annotated with EnumSerializable:
import 'package:json_annotation/json_annotation.dart';
import 'package:mobkit_enum_generator/annotations.dart';
part 'example.g.dart';
@EnumSerializable(String, null)
enum PersonStr {
@JsonValue('John')
name,
@JsonValue('66')
number;
static PersonStr? fromJson(dynamic json) => _$PersonStrFromJson(json);
}
@EnumSerializable(int, ["name description", "number description"])
enum PersonInt {
@JsonValue(1)
name,
@JsonValue(66)
number;
static PersonInt? fromJson(dynamic json) => _$PersonIntFromJson(json);
}
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]!;
}
}
PersonStr? _$PersonStrFromValue(String id) {
return _$PersonStrEnumMap.keys
.any((element) => _$PersonStrEnumMap[element] == id)
? _$PersonStrEnumMap.keys
.firstWhere((element) => _$PersonStrEnumMap[element] == id)
: null;
}
PersonStr? _$PersonStrFromJson(dynamic json) {
if (json is String) {
return _$PersonStrFromValue(json);
}
return null;
}
const Map<PersonInt, int> _$PersonIntEnumMap = {
PersonInt.name: 1,
PersonInt.number: 66,
};
extension PersonIntExtension on PersonInt {
int toValue() {
return _$PersonIntEnumMap[this]!;
}
}
PersonInt? _$PersonIntFromValue(int id) {
return _$PersonIntEnumMap.keys
.any((element) => _$PersonIntEnumMap[element] == id)
? _$PersonIntEnumMap.keys
.firstWhere((element) => _$PersonIntEnumMap[element] == id)
: null;
}
PersonInt? _$PersonIntFromJson(dynamic json) {
if (json is int) {
return _$PersonIntFromValue(json);
}
return null;
}
extension PersonIntDescriptionExtension on PersonInt {
String toDescription() {
switch (this) {
case PersonInt.name:
return 'name description';
case PersonInt.number:
return 'number description';
}
}
}
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_enum_generator/annotations.dart';
part 'example.g.dart'
@EnumSerializable(String, null)
enum Person {
@EnumValue('John')
name,
@JsonValue('66')
number;
static Person? fromJson(dynamic json) => _$PersonFromJson(json);
}