Skip to content

Latest commit

 

History

History
51 lines (41 loc) · 1.14 KB

README.md

File metadata and controls

51 lines (41 loc) · 1.14 KB

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.