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

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

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 = BobsBlocState<RandomNumberFetchException, int>;

// Create your cubit with the BobsCubitMixin.
class RandomNumberFetchCubit extends Cubit<RandomNumberFetchState>
    with BobsCubitMixin {
  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 => Text(
          switch (state.failure!) {
            .unknown => 'Unknown error occurred.',
            .overflow => 'Error: Overflowed',
          },
        ),
    },
),

For more flexibility #

// Extend BobsBlocState instead of using typedef.
class RandomNumberFetchState
    extends BobsBlocState<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));
  }
}
1
likes
160
points
240
downloads

Publisher

unverified uploader

Weekly Downloads

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

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

bloc, bobs_jobs

More

Packages that depend on bobs_bloc