Skip to content
| Marketplace
Sign in
Visual Studio Code>Other>Dart Union ClassNew to Visual Studio Code? Get it now.
Dart Union Class

Dart Union Class

Ascenio

|
1,849 installs
| (0) | Free
generates extensions to make unions possible
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

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

:muscle: 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.

  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
© 2025 Microsoft