Generates simple extension methods to simulate unions in Dart. No need for code generation, because I'm sure we all love that.
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');
},
);
}
Totally original idea right there. Definitely not inspired by Dart Data Class and Freezed.