Take one ordinary screen: fetch a user from an API and show it. Every app has to handle three outcomes:
it worked
it's still loading
it failed
BLoC and Riverpod both handle all three. The difference is who writes the plumbing .
BLoC: you define every state
With BLoC you model the states yourself and emit them.
sealed class UserState {} class UserLoading extends UserState {} class UserLoaded extends UserState { UserLoaded ( this . user ); final User user ; } class UserError extends UserState { UserError ( this . message ); final String message ; }
Enter fullscreen mode
Exit fullscreen mode
class UserCubit extends Cubit < UserState > { UserCubit ( this . repo ) : super ( UserLoading ()); final UserRepository repo ;
Future < void > load () async { emit ( UserLoading ()); try { emit ( UserLoaded ( await repo . fetchUser ())); } catch ( e ) { emit ( UserError ( e .…