enum_ext 1.4.0 copy "enum_ext: ^1.4.0" to clipboard
enum_ext: ^1.4.0 copied to clipboard

Lets make enums super useful by adding some useful extension methods. Say good bye to switch-case and if-else blocks.

example/main.dart

import 'package:enum_ext/enum_ext.dart';

part 'main.g.dart';

@EnumExt(
  conditionalGetters: false,
)
enum HttpResponse {
  @EnumExtValue(200)
  ok,
  @EnumExtValue(404)
  notFound,
  @EnumExtValue(500)
  internalServerError,
}

void main(List<String> arguments) {
  final response = HttpResponse.internalServerError;

  print(response); // HttpResponse.ok
  print(response.name); // ok
  print(response.index); // 0
  print(response.extValue); // 200

  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 skinType = response.mapSimply(
    ok: "Some value based on HttpResponse.ok",
    notFound: "Some value based on HttpResponse.notFound",
    internalServerError: "Some value based on HttpResponse.internalServerError",
  );

  print(response.title); // Internal Server Error
}
13
likes
160
pub points
30%
popularity

Publisher

unverified uploader

Lets make enums super useful by adding some useful extension methods. Say good bye to switch-case and if-else blocks.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

More

Packages that depend on enum_ext