flutter_bloc meets sealed_unions
Our top sponsors are shown below! [Become a Sponsor]
Try the Flutter Chat Tutorial 💬 |
||
class MyState extends Union4Impl<Initial, Loading, Success, Failure> {
static final unions = const Quartet<Initial, Loading, Success, Failure>();
MyState._(Union4<Initial, Loading, Success, Failure> union) : super(union);
factory MyState.initial() => MyState._(unions.first(Initial()));
factory MyState.loading() => MyState._(unions.second(Loading()));
factory MyState.success({required String data}) =>
MyState._(unions.third(Success(data: data)));
factory MyState.failure({required String error}) =>
MyState._(unions.fourth(Failure(error: error)));
}
class Initial {}
class Loading {}
class Success {
final String data;
Success({required this.data});
}
class Failure {
String error;
Failure({required this.error});
}
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SealedBlocBuilder4<MyBloc, MyState, Initial, Loading, Success,
Failure>(
builder: (context, states) => states(
(initial) => Text('Initial'),
(loading) => CircularProgressIndicator(),
(success) => Text('Success: ${success.data}'),
(failure) => Text('Failure: ${failure.error}'),
),
);
}
}
More examples can be found at sealed_flutter_bloc_samples.