reusable_bloc_base 0.1.0 copy "reusable_bloc_base: ^0.1.0" to clipboard
reusable_bloc_base: ^0.1.0 copied to clipboard

A very good base for BloC user, ready to use, with many prebuilt useful functions.

reusable_bloc_base #

Light-weight extension library for flutter_bloc

Features #

Cover most of the common work flow in a flutter bloc, avoid boilerplate code.

Getting started #

Install the package

    flutter pub add reusable_bloc_base

Usage #

    // Your state class
    final class HelloWorldState extends BaseState<HelloWorldLoading> {
      const HelloWorldState({
        super.failure,
        super.isLoading,
        super.pendingActions,
        super.success,
      });

      @override
      List<Object> get props => [
            ...super.props,
          ];

      factory HelloWorldState.initial() {
        return const HelloWorldState();
      }

      @override
      HelloWorldState copyWith({
        AnySuccess? success,
        AnyFailure? failure,
        bool? isLoading,
        Set<HelloWorldLoading>? pendingActions,
      }) {
        return HelloWorldState(
          failure: failure ?? this.failure,
          isLoading: isLoading ?? this.isLoading,
          pendingActions: pendingActions ?? this.pendingActions,
          success: success ?? this.success,
        );
      }

      @override
      String toString() {
        return '''
          HelloWorldState {
            failure: $failure,
            isLoading: $isLoading,
            pendingActions: $pendingActions,
            success: $success,
          }
          ''';
      }
    }
    // Your bloc class
    class HelloWorldBloc extends BaseBloc<HelloWorldLoading,
    HelloWorldEvent, HelloWorldState> {
        HelloWorldBloc() { 
            ...//Your intialization code
        }

        FutureOr<void> _onHelloWorldEvent(
          HelloWorldEvent event,
          Emitter<HelloWorldState> emit,
        ) async {
            emit(state.beforeLoading()); // <--- you can just call this to set isLoading to true
            try {
                // Your business logic here ... (synchronous or asynchronous)
                emit(state.successState<HelloWorldState>()); // <--- state.successState will set isLoading to false and success to the given value
            } catch (e, stack) {
                addError(e, stack); // <--- simply call this to set failure state, the details of error will reflect in the state.failure (as long as you are using any_state package)
            }
        }
    } 

Additional information #

Feel free to try it out and let me know what you think. I'm open to suggestions and improvements. Contact me at my email

0
likes
160
points
49
downloads

Publisher

unverified uploader

Weekly Downloads

A very good base for BloC user, ready to use, with many prebuilt useful functions.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

any_state, bloc_concurrency, equatable, flutter, flutter_bloc

More

Packages that depend on reusable_bloc_base