good_enums 0.2.0 copy "good_enums: ^0.2.0" to clipboard
good_enums: ^0.2.0 copied to clipboard

Good enum code generation

Good Enums #

Code generator to make enums easier to use. Annotations lib here.

Setup #

See the example for the configuration.

dependencies:
  flutter:
    sdk: flutter
  good_enums_annotations:

dev_dependencies:
  build_runner:
  good_enums:

Example #

Annotate the enum.

import 'package:good_enums_annotations/good_enums_annotations.dart';
part 'environment.g.dart';

@goodEnum
enum Environment {
  dev,
  prod,
  stage;
}

Functions created:

  • For each field an isField method
if (environment.isDev()) {
  // do
  return;
}
// else

if valueCheckAsGetter is set to true

if (environment.isDev) {
  // do
  return;
}
// else
  • Method ifIs with a callback for each enum field:
return environment.ifIs(
          dev: () => Text("IsDev"),
          prod: () => Text("IsProd"),
          stage: () => Text("IsStage"),
        );
  • Method maybeIfIs with an optional callback for each enum field and an else case:
return environment.maybeIfIs(
          dev: () => Text("IsDev"),
          orElse: () => Text("IsElse"),
        );

Code generated:

extension GoodEnvironment on Environment {
  bool isDev() {
    return this == Environment.dev;
  }

  bool isProd() {
    return this == Environment.prod;
  }

  bool isStage() {
    return this == Environment.stage;
  }

  TResult ifIs<TResult>({
    required TResult Function() dev,
    required TResult Function() prod,
    required TResult Function() stage,
  }) {
    switch (this) {
      case Environment.dev:
        return dev();
      case Environment.prod:
        return prod();
      case Environment.stage:
        return stage();
    }
  }

  TResult maybeIfIs<TResult>({
    TResult Function()? dev,
    TResult Function()? prod,
    TResult Function()? stage,
    required TResult Function() orElse,
  }) {
    switch (this) {
      case Environment.dev:
        if (dev != null) return dev();
        break;
      case Environment.prod:
        if (prod != null) return prod();
        break;
      case Environment.stage:
        if (stage != null) return stage();
        break;
    }
    return orElse();
  }
}

Settings #

GoodEnum can be configured.

@GoodEnum(
  // disable/enable isIs method, default: true
  enableIf: true,
  // disable/enable maybeIfIs method, default: true
  enableMaybeIf: true,
  // customize prefix methods, default `iS`
  prefix: 'iS'
  valueCheckAsGetter:false,
)
...
0
likes
120
pub points
0%
popularity

Publisher

unverified uploader

Good enum code generation

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (LICENSE)

Dependencies

analyzer, build, good_enums_annotations, source_gen

More

Packages that depend on good_enums