Menu

One network request, two models: BLoC states vs Riverpod's AsyncValue
📰

One network request, two models: BLoC states vs Riverpod's AsyncValue

DEV Community: dart·Stacklyn Labs·1 day ago
#mqsrYkK3
#devtodart#devto#dev#user#bloc
Reading 0:00
15s threshold

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 .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.