enum_ext

Last updated:

0 purchases

enum_ext Image
enum_ext Images
Add to Cart

Description:

enum ext

Lets make enums ๐Ÿš€ by adding some useful extension methods.
Say good bye to switch-case and if-else blocks.
Inspired by freezed
Features #


Generate Some Useful Methods #

when
mayBeWhen
onlyWhen
map
mapSimply
mayBeMap



Generate Value Checking Getters #


Generate Custom Extension Value #


Title case of enum name #


Getting started #
Add following dependencies -
dependencies:
yaml: ^3.1.0
enum_ext: ^1.0.0

dev_dependencies:
build_runner:
enum_ext_gen: ^1.0.0
copied to clipboard
Usage #
Add the part directive in the file
part 'file_name.g.dart';
copied to clipboard
Extension methods #
In order to generate extension methods, simply annotate the enum with @EnumExt()
@EnumExt()
enum HttpResponse { ok, notFound, internalServerError }
copied to clipboard
โš™๏ธ Now generate the code using build_runner.

Dart only -
dart pub run build_runner build
// OR
dart pub run build_runner watch
copied to clipboard
Flutter -
flutter pub run build_runner build
// OR
flutter pub run build_runner watch
copied to clipboard
This will generate all these extension methods for you
final response = HttpResponse.internalServerError;

response.when(
ok: (e) {
// Do some actions only if the response is HttpResponse.ok
},
notFound: (e) {
// Do some actions only if the response is HttpResponse.notFound
},
internalServerError: (e) {
// Do some actions only if the response is HttpResponse.internalServerError
},
);

response.mayBeWhen(
ok: (e) {
// Do some actions only if the response is HttpResponse.ok
},
orElse: (e) {
// Do some actions only if the response is something other than HttpResponse.ok
},
);

response.onlyWhen(
ok: (e) {
// Do some actions only if the response is HttpResponse.ok
},
);

final value = response.map(
ok: (e) => "Some value based on HttpResponse.ok",
notFound: (e) => "Some value based on HttpResponse.notFound",
internalServerError: (e) => "Some value based on HttpResponse.internalServerError",
);

final something = response.mapSimply(
ok: "Some value based on HttpResponse.ok",
notFound: "Some value based on HttpResponse.notFound",
internalServerError: "Some value based on HttpResponse.internalServerError",
);
copied to clipboard

Note :- when(), onlyWhen(), mayBeWhen() methods used when do just want to perform some task based on specific enum properties, whereas map(), mayBeMap(), mapSimply() methods are used to return a value based on the enum.

Value Checking Getters #
It will also generate value checking getter like
response.isOk // response == Response.ok
response.isNotOk // response != Response.ok
copied to clipboard
If you do not want to generate value checking getters, you can set conditionalGetters: false like this
EnumExt(conditionalGetters: false)
copied to clipboard
Extension value #
In order to generate extension value, annotate enum properties with @EnumExtValue(...)
โš ๏ธNote:- Any enum propery that is not annotated with @EnumExtValue(...) will simply return null
โš ๏ธNote:- Supported values for @EnumExtValue(...) are String, int, double, List, Map, Set and Symbol. Any other vaue will be replaced by null
Lets add some extension values to the HttpResponse enum
@EnumExt()
enum HttpResponse {
@EnumExtValue(200)
ok,
@EnumExtValue(404)
notFound,
@EnumExtValue(500)
internalServerError,
}
copied to clipboard
This will generate a getter extension named extValue on the enum.
final response = HttpResponse.internalServerError;

print(response); // HttpResponse.internalServerError
print(response.index); // 2
print(response.name); // internalServerError
print(response.extValue); // 500 <-- ๐Ÿ”ฅ Extension
copied to clipboard
and
HttpResponse.values.byExtValue(500);// returns HttpResponse.internalServerError
copied to clipboard
Title case of enum name #
It also generate title getter extension in order to get Title Case of enum property name.
@EnumExt()
enum HttpResponse { ok, notFound, internalServerError }

void main() {
final response = HttpResponse.internalServerError;

print(response.title); // Internal Server Error
}
copied to clipboard
Please hit a like๐Ÿ‘ if this package made your life ๐Ÿš€ #

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.