Last updated:
0 purchases
good enums
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:
copied to clipboard
Example #
Annotate the enum.
import 'package:good_enums_annotations/good_enums_annotations.dart';
part 'environment.g.dart';
@goodEnum
enum Environment {
dev,
prod,
stage;
}
copied to clipboard
Functions created:
For each field an isField method
if (environment.isDev()) {
// do
return;
}
// else
copied to clipboard
if valueCheckAsGetter is set to true
if (environment.isDev) {
// do
return;
}
// else
copied to clipboard
Method ifIs with a callback for each enum field:
return environment.ifIs(
dev: () => Text("IsDev"),
prod: () => Text("IsProd"),
stage: () => Text("IsStage"),
);
copied to clipboard
Method maybeIfIs with an optional callback for each enum field and an else case:
return environment.maybeIfIs(
dev: () => Text("IsDev"),
orElse: () => Text("IsElse"),
);
copied to clipboard
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();
}
}
copied to clipboard
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,
)
...
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.