mobkit_enum_generator 0.1.6 copy "mobkit_enum_generator: ^0.1.6" to clipboard
mobkit_enum_generator: ^0.1.6 copied to clipboard

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: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,
}

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 '';
    }
  }
}

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,
}
13
likes
140
pub points
68%
popularity

Publisher

verified publisherbiscozum.com.tr

Provides Dart Build System builders for handling Enum. You can get values ​and descriptions from your enums.

Repository (GitHub)
View/report issues

Topics

#enum #generate #json #helper #serializable

Documentation

API reference

License

BSD-3-Clause (LICENSE)

Dependencies

analyzer, build, build_runner, flutter, source_gen

More

Packages that depend on mobkit_enum_generator