bloc_wrapped_values 1.1.0 copy "bloc_wrapped_values: ^1.1.0" to clipboard
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 #

Pub Version

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, and error 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 and maybeWhen 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 with AsyncValueWrapper (initial, loading, success, error)
  • ErrorValueCubit with ErrorValueWrapper (value, error)

Contributing #

Contributions are welcome! Please feel free to submit an Issue or a Pull Request.

License #

See the LICENSE file for details.

2
likes
160
points
27
downloads

Publisher

unverified uploader

Weekly Downloads

A package that provides a clean and type-safe way to handle (async) states in BLoC/Cubit patterns.

Repository (GitHub)
View/report issues

Topics

#bloc #cubit #state-management #async

Documentation

Documentation
API reference

License

MIT (license)

Dependencies

bloc

More

Packages that depend on bloc_wrapped_values