Last updated:
0 purchases
mobkit enum generator
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
copied to clipboard
Example #
Given a library example.dart with an Person enum annotated with EnumSerializable:
import 'package:mobkit_enum_generator/annotations.dart';
part 'example.g.dart'
@EnumSerializable(String, null)
enum PersonStr {
@JsonValue('John')
name,
@JsonValue('66')
number,
}
@EnumSerializable(int, ["name description", "number description"])
enum PersonInt {
@JsonValue(1)
name,
@JsonValue('66')
number,
}
copied to clipboard
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]!;
}
}
extension PersonIntDescriptionExtension on PersonInt {
String toDescription() {
switch (this) {
case PersonInt.name:
return 'name description';
case PersonInt.number:
return 'number description';
default:
return '';
}
}
}
copied to clipboard
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,
}
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.