Bob's bloc

A package that takes advantage of BobsJobs to radically simplify bloc applications.

coverage pub package style: very good analysis license: BSD 3

🕹️ Usage

// Define a your cubit state as a typedef.
typedef RandomNumberFetchState =
    BobsRequestCubitState<RandomNumberFetchException, int>;

// Create your cubit with the BobsRequestCubitMixin.
class RandomNumberFetchCubit extends Cubit<RandomNumberFetchState>
    with BobsRequestCubitMixin {
  RandomNumberFetchCubit({required this.randomRepository}) : super(.initial());

  final RandomRepository randomRepository;

  // Make a request call and pass in your job. Everything else is handled for you.
  Future<void> fetchRandomNumber() =>
      request(randomRepository.fetchRandomNumber());
}

// ...

// Update your UI based on the current request state.
BlocBuilder<RandomNumberFetchCubit, RandomNumberFetchState>(
    builder: (context, state) => switch (state.status) {
        .initial => const Text('Generate a random number!'),
        .inProgress => const CircularProgressIndicator(),
        .succeeded => Text('${state.success}'),
        .failed => switch (state.failure!) {
            .unknown => const Text('Unknown error occurred.'),
            .overflow => const Text('Error: Overflowed'),
        },
    },
),

For more flexibility

// Extend BobsRequestCubitState instead of using typedef.
class RandomNumberFetchState
    extends BobsRequestCubitState<RandomNumberFetchException, int> {
  RandomNumberFetchState.initial() : super.initial();

  RandomNumberFetchState.inProgress() : super.inProgress();

  RandomNumberFetchState.completed(super.outcome) : super.completed();
}

class RandomNumberFetchCubit extends Cubit<RandomNumberFetchState> {
  RandomNumberFetchCubit({required this.randomRepository}) : super(.initial());

  final RandomRepository randomRepository;

  // Emit the state changes manually.
  Future<void> fetchRandomNumber() async {
    if (state.isInProgress) return;

    emit(.inProgress());

    final outcome = await randomRepository.fetchRandomNumber().run();

    emit(.completed(outcome));
  }
}

Libraries

bobs_bloc