sub_state 0.0.3 copy "sub_state: ^0.0.3" to clipboard
sub_state: ^0.0.3 copied to clipboard

A lightweight state management helper built on top of bloc and freezed.

SubState & CubitSubState #

A lightweight state management helper built on top of bloc and freezed.
It provides a simple way to represent loading, success, failure, and initial states in your Cubits.


πŸš€ Features #

  • SubState with four states: initial, loading, success, failure
  • Convenience getters: isInitial, isLoading, isSuccess, isFailure
  • Safe accessors: dataOrNull, errorOrNull
  • CubitSubState base class with helper emit methods

πŸ“¦ Installation #

Add to your pubspec.yaml:

dependencies:
  bloc: ^8.1.0
  freezed_annotation: ^2.4.1
  # Your package (when published):
  sub_state: ^0.0.1

Then run:

flutter pub get

And don’t forget to run the code generator if you edit the freezed parts:

flutter pub run build_runner build --delete-conflicting-outputs

πŸ›  Usage #

Example Cubit #

class UserCubit extends CubitSubState<String> {
  Future<void> fetchUser() async {
    emitLoading();
    await Future.delayed(const Duration(seconds: 1));
    emitSuccess("John Doe");
  }

  void failFetch() {
    emitFailure("Something went wrong");
  }
}

Example Widget #

BlocBuilder<UserCubit, SubState<String>>(
  builder: (context, state) {
    if (state.isLoading) {
      return const CircularProgressIndicator();
    } else if (state.isSuccess) {
      return Text("Hello, ${state.dataOrNull}");
    } else if (state.isFailure) {
      return Text("Error: ${state.errorOrNull}");
    }
    return const Text("Initial");
  },
);

πŸ“„ License #

This project is licensed under the MIT License.

2
likes
150
points
91
downloads

Publisher

verified publisheralghanem.de

Weekly Downloads

A lightweight state management helper built on top of bloc and freezed.

Repository (GitHub)
View/report issues

Documentation

Documentation
API reference

License

MIT (license)

Dependencies

flutter, flutter_bloc, freezed_annotation

More

Packages that depend on sub_state