bloc_wrapped_values 1.1.0
bloc_wrapped_values: ^1.1.0 copied to clipboard
A package that provides a clean and type-safe way to handle (async) states in BLoC/Cubit patterns.
Bloc Wrapped Values #
A Flutter package that provides a clean and type-safe way to handle (async) states in BLoC/Cubit patterns. Just wrap your values with loading, success, and error states to build reactive UIs with ease.
Features #
- π State Tracking: Easily track
loading
,success
, anderror
states - π Type Safety: Fully type-safe API for your data models
- π§© No dependency: Just
bloc
and dart - π― Simplified State Emission: Helper methods to
emit
different states - π Pattern Matching: Convenient
when
andmaybeWhen
methods for UI rendering
Installation #
Add this to your package's pubspec.yaml
file:
dependencies:
bloc_wrapped_values: ^1.1.0
Basic Example #
class UserCubit extends AsyncValueCubit<User> {
UserCubit() : super();
Future<void> fetchUser(String id) async {
emitLoading();
try {
final user = await userRepository.getUser(id);
emitSuccess(user);
} catch (error) {
emitError(error);
}
}
}
or with an even more convenient way using emitGuarded
method
class UserCubit extends AsyncValueCubit<User> {
UserCubit() : super();
Future<void> fetchUser(String id) async {
emitGuarded(() {
return userRepository.getUser(id);
});
}
}
And use it in your UI with when
's Pattern Matching like
BlocBuilder<UserCubit, AsyncValueWrapper<User>>(
builder: (context, state) {
return state.when(
initial: () => InitialWidget(),
loading: (oldUser) => UserLoadingWidget(oldUser: oldUser),
success: (user) => UserWidget(user: user),
error: (err, oldUser) => UserErrorWidget(err: err, oldUser: oldUser),
);
},
)
And if you don't want to handle all the cases, AsyncValueWrapper
provides when
and maybeWhen
methods for easy use
// Handle only success state, with fallback for others
state.maybeWhen(
success: (data) => SuccessWidget(data: data),
orElse: () => DefaultWidget(),
);
Available helpers #
AsyncValueCubit
withAsyncValueWrapper
(initial
,loading
,success
,error
)ErrorValueCubit
withErrorValueWrapper
(value
,error
)
Contributing #
Contributions are welcome! Please feel free to submit an Issue or a Pull Request.
License #
See the LICENSE file for details.