Skip to content

Generate simple extension methods to simulate unions in Dart 💙

License

Notifications You must be signed in to change notification settings

Ascenio/dart-union-class

Repository files navigation

Dart Union Class

Generates simple extension methods to simulate unions in Dart. No need for code generation, because I'm sure we all love that.

demo

💪 Features

map

You can act differently on each possible value.

void main() {
  final state = getUser();
  state.map(
    loadingState: (_) {
      print('Loading..');
    },
    loadedState: (_) {
      print("You're good to go!");
    },
    errorState: (_) {
      print('Oops, something happened!');
    },
  );
}

But also return values accordingly.

Widget build(BuildContext context) {
  final state = getUser();
  return state.map<Widget>(
    loadingState: (_) {
      return CircularProgressIndicator();
    },
    loadedState: (loaded) {
      return Text('Hello, ${loaded.userName}!');
    },
    errorState: (error) {
      return Text('Something bad happened: $error');
    },
  );
}

:shipit: Inspiration

Totally original idea right there. Definitely not inspired by Dart Data Class and Freezed.

About

Generate simple extension methods to simulate unions in Dart 💙

Topics

Resources

License

Stars

Watchers

Forks