bloc_plus
bloc_plus adds two things to flutter_bloc: async helpers for Cubits, with
"latest wins" runs and emits that stop at close(), and one-off effects such
as navigation or a snack bar, kept out of the state. It also has rebuild and
listen conditions, and widgets that hand the bloc to their callbacks.
Features
- Async helpers for Cubits:
RestartableTasksMixin(runLatestOutcome,runLatest,cancelLatest,isTaskRunning): "latest wins" runs by key, cancelled atclose()SafeEmitMixin(Cubits only):safeEmitandguardedTaskOutcome,CancellationToken(withwhenCancelled) andRestartableTask(runOutcome) for "latest wins" outside a bloc
- Effects, one-off events kept out of the state:
HasEffects(emitEffect)BlocEffectListener, which composes insideMultiBlocListenerBlocConsumerWithEffects: the state and the effects of one bloc in one widgetdebugPrintUnheardEffects, an opt-in debug warning for effects that nothing listens to
- Conditions for
buildWhen,listenWhenandselectorShouldRebuild:BlocCondition,whenChanged - Widgets that hand the bloc to their callbacks:
BlocBuilderWithBloc,BlocListenerWithBloc,BlocConsumerWithBloc,BlocSelectorWithBloc - Deprecated, removed in 1.0:
RebuildPolicy,ListenPolicyand their factories,EffectListener,MultiEffectListener,SingleChildEffectListener(since 0.3.0); theBuildContexthelpersreadOrNull,watchOrNull,selectOrNullandwithBloc,effectWhenwithEffectCondition,RestartableTask.run,BlocEffectListener.withChild, and the typedefsBlocEffectListenerWithBlocandBlocSelectedCondition(since 0.4.0)
Delivery notes, next steps and guardrails are tracked in
docs/library_improvement_plan.md.
Getting started
Add dependency:
dependencies:
bloc_plus: ^0.4.0
Run the example app:
cd example
flutter pub get
flutter run
Usage
Async helpers for Cubits
| You want | Use |
|---|---|
an emit after an await that does nothing once the cubit is closed |
safeEmit from SafeEmitMixin |
| the result of work that may outlive the cubit, used afterwards | guarded from SafeEmitMixin, for a non-null result |
| "latest wins": a new call supersedes the one that is still running | runLatestOutcome from RestartableTasksMixin, or runLatest when the result is non-null |
| "latest wins" outside a bloc | RestartableTask |
| no further logic, not only no emit, once the cubit is closed | a plain if (isClosed) return;, which this package does not replace |
"Latest wins" needs RestartableTasksMixin only:
class SearchCubit extends Cubit<SearchState>
with RestartableTasksMixin<SearchState> {
SearchCubit(this.repository) : super(const SearchState());
final SearchRepository repository;
Future<void> search(String query) async {
// Before the first await the cubit may be closed already; safeEmit from
// SafeEmitMixin does this check for you.
if (isClosed) return;
emit(state.copyWith(isLoading: true, failed: false));
final TaskOutcome<Item?> outcome;
try {
outcome = await runLatestOutcome('search', (token) {
return repository.find(query, abortTrigger: token.whenCancelled);
});
} catch (_) {
// Only the failure of the latest run arrives here.
emit(state.copyWith(isLoading: false, failed: true));
return;
}
// A superseded call resumes at once while the newer search is still
// loading, and close() cancels the run: only a completed outcome clears
// the flag.
if (!outcome.isCompleted) return;
emit(SearchState(item: outcome.value)); // item is null when not found
}
}
safeEmit matters for an emit that can run on a closed cubit, such as the
one before the first await when search is called after close(). After
runLatestOutcome, a closed cubit gives a cancelled outcome, also when the
run failed just before close(), so the check of outcome.isCompleted covers
it and the catch never runs on a closed cubit.
runLatestOutcome and RestartableTask.runOutcome complete with a
TaskOutcome: TaskOutcome.completed(value) for the latest run, or
TaskOutcome.cancelled() as soon as the run is superseded, cancelled
(cancelLatest, cancelAllLatest, RestartableTask.cancel), disposed or the
bloc closes. outcome.value throws on a cancelled outcome, so check
outcome.isCompleted first, or match it with
if (outcome case TaskOutcome(isCompleted: true, :final value)), with
isCompleted: true before value, because a pattern reads its fields in
order. Both methods are @useResult: the analyzer reports an outcome that is
awaited and dropped, because the code after it would run for a cancelled run
as well. Outcomes compare by value, so
expect(outcome, const TaskOutcome<int>.completed(2)) works in tests.
- The abandoned task keeps running. Cancellation releases the caller and
drops the result, but does not stop the work. Check
token.isCancelledbefore any side effect (a cache write, an analytics call), or passtoken.whenCancelledto the I/O so it can abort:package:httpaccepts it asabortTrigger, and for dio callcancel()on aCancelTokenfromtoken.whenCancelled.then(...). - A superseded caller resumes immediately, while the newer run is still
in flight. Code after the
await, including afinallyblock that resets state shared with the newer run (such as a loading flag), must check the outcome first. - Failures are not an outcome. The failure of the latest run is rethrown,
so use
try/catch. A failure of a cancelled run is dropped, also one that arrived just before the cancel, while the caller had not resumed yet. - Inside
Blocevent handlers, prefer the handler'semitwith abloc_concurrencytransformer such asrestartable(). If a handler does callrunLatest, it now completes as soon as the run is cancelled, but the abandoned task can still call the handler'semit: bloc rejects that call only with an assert, so release builds emit the stale state, and in debug the assert's error is dropped along with the abandoned task's other failures. Checkemit.isDone(ortoken.isCancelledwithrunLatestOutcome) before callingemitinside the task. SafeEmitMixinis for Cubits. ABloccannot mix it in: the handler'semitalso callsonTransitionand does nothing in a handler that a transformer has cancelled, whichsafeEmitskipped. In a handler, checkisClosedfirst and again after everyawait, beforeemitand before starting work: it istruefrom the call ofclose(), while the handler'semitstill delivers for a while, also in a handler that starts afterclose()for an event queued before it. Withemit.onEach, check it inonDataandonErrortoo;emit.forEachemits every state its callbacks return, so a check there cannot stop the emit: where a value must not arrive afterclose(), useemit.onEach. The 0.4.0 entry of the CHANGELOG has the full migration.- Only the bloc calls them.
isTaskRunning,runLatest,runLatestOutcome,cancelLatest,cancelAllLatest,safeEmitandguardedare@protectedand@visibleForTesting, likeemit: the bloc, its subclasses, amixin X on RestartableTasksMixin<S>oron SafeEmitMixin<S>and tests call them, while a call from a widget, an extension on the bloc or a function outside the class gets analyzer warnings. Keep what the UI shows, such as a loading flag, in the state rather than readingisTaskRunninginbuild. A method that cancels the run clears that flag itself: the example above clears it only when the run completes or fails.
runLatest, CancellationToken.run and guarded complete with null when
the work was superseded, cancelled or outlived the bloc, also when that work
fails, so only the latest run can throw. Handle null first, with an early
return: an else or a default branch would take discarded work for a failed
result. Like the outcome methods, they are @useResult: the analyzer reports
a result that is awaited and dropped. The task of runLatest gets no token:
a task that checks token.isCancelled or passes token.whenCancelled to its
I/O uses runLatestOutcome.
For void work or a nullable result, null would be ambiguous, so
runLatest, CancellationToken.run and guarded do not accept it
(T extends Object). RestartableTask.run cannot take that bound, because
its T belongs to the class, so it is deprecated and will be removed in 1.0.
Instead of runLatest or RestartableTask.run, use runLatestOutcome as
above or RestartableTask.runOutcome, and check the outcome before anything
else. Instead of guarded or CancellationToken.run, await such work plainly
and then check what the method checked, before anything else: isClosed for
guarded, which
safeEmit checks for you, and token.isCancelled for
CancellationToken.run, which safeEmit does not check:
// Instead of guarded:
final item = await repository.find(q); // may be null: not found
safeEmit(state.copyWith(item: item));
// Instead of CancellationToken.run:
final found = await repository.find(q);
if (token.isCancelled) return;
safeEmit(state.copyWith(item: found));
Wrap the result in a record only to keep what these two add: the work does
not start on a closed bloc or a cancelled token, its later failure is
dropped, and CancellationToken.run releases its caller at cancel()
instead of when the work ends:
final result = await guarded(() async => (item: await repository.find(q)));
if (result == null) return; // the bloc was closed
safeEmit(state.copyWith(item: result.item)); // item may be null: not found
The cancellation_token package also exports a CancellationToken. If you
import both, add hide CancellationToken to one import or use a prefix.
CancellationToken and RestartableTask are final classes, so they cannot
be extended, implemented or mocked. In tests, use real instances, for example
CancellationToken()..cancel() for a token that is already cancelled.
A flow with several awaits
Run the whole flow inside one runLatestOutcome, check token.isCancelled
after every await and emit inside the task. A catch inside the task needs
the same check first: the abandoned flow keeps running and can fail after the
newer flow has emitted, and without the check its stale failure replaces the
newer state. The method returns the future of runLatestOutcome and is not
async: awaiting the future as a statement would drop the outcome, which the
analyzer reports. In a method that does more, such as one with a try around
the call, keep await and check the outcome instead: a returned future
escapes the catch and the finally.
Future<void> open(String orderId) {
return runLatestOutcome('open', (token) async {
try {
final order = await repository.order(orderId);
if (token.isCancelled) return;
// A new order starts without an invoice.
emit(OrderState(order: order));
final invoice = await repository.invoice(order);
if (token.isCancelled) return;
emit(state.copyWith(invoice: invoice));
} catch (_) {
// A superseded flow can fail after the newer one has emitted.
if (token.isCancelled) return;
emit(state.copyWith(failed: true));
}
});
}
A key names a whole flow, not a step. With one run per step under the same
key, a superseded flow that has any other await between its steps resumes
and starts its next step: that step cancels the newer flow when the newer
flow is inside a run, and emits its stale state otherwise. close() cancels
the token as well, so the same checks stop the flow on a closed cubit.
Ignore while running
To ignore a call while the previous one still runs, check isTaskRunning
before the first await:
Future<void> refresh() async {
if (isTaskRunning('refresh')) return;
final outcome = await runLatestOutcome('refresh', (_) => repository.load());
if (!outcome.isCompleted) return;
emit(state.copyWith(items: outcome.value));
}
isTaskRunning is not reactive, so read it inside the cubit, not in build.
For the handlers of a Bloc, use droppable() from bloc_concurrency.
Effects
Model effects as a sealed hierarchy and emit them from inside the bloc:
sealed class AuthEffect {}
final class ShowError extends AuthEffect {
ShowError(this.message);
final String message;
}
final class GoHome extends AuthEffect {}
class AuthCubit extends Cubit<AuthState> with HasEffects<AuthState, AuthEffect> {
AuthCubit() : super(const AuthState());
void signedIn() => emitEffect(GoHome());
}
Listen with BlocEffectListener<B, E> and handle every effect with an
exhaustive switch. It composes inside MultiBlocListener next to
BlocListener and BlocListenerWithBloc:
MultiBlocListener(
listeners: [
BlocListener<AuthCubit, AuthState>(listener: (context, state) {}),
BlocEffectListener<AuthCubit, AuthEffect>(
onEffect: (context, effect) {
switch (effect) {
case ShowError(:final message):
showError(context, message);
case GoHome():
goHome(context);
}
},
),
],
child: const AuthView(),
)
- Write both type arguments. Without them the
effectparameter ofonEffectisObject?at every language version, so theswitchis not checked for exhaustiveness. - To react to one subtype, listen to the supertype and use an if-case
pattern at the top of
onEffect, which also promotes the effect type:if (effect case ShowError(:final message)) .... MultiBlocListenerasserts thatlistenersis not empty, and ignores achildpassed to a listener inside it.emitEffectis@protectedand@visibleForTesting, likeemit. Calls from outside the bloc, including extensions on the bloc and other libraries, get analyzer warnings. Share helpers between blocs with amixin AuthEffects on HasEffects<AuthState, AuthEffect>.- In a
Blochandler that a transformer such asrestartable()has cancelled,emitEffectstill delivers its effect: checkemit.isDonefirst.
Migrating from the deprecated APIs (removed in 1.0):
| Before | After |
|---|---|
EffectListener<B, S, E> |
BlocEffectListener<B, E> |
MultiEffectListener |
MultiBlocListener |
EffectListener on a subtype of the declared effect type |
BlocEffectListener on the supertype with an if-case pattern |
effectWhen: (effect) => effect is ShowError (deprecated in 0.4.0) |
if (effect case ShowError(...)) or if (effect is! ShowError) return; at the top of onEffect |
listener.withChild(child) (deprecated in 0.4.0) |
child: in the constructor, or the listener in MultiBlocListener |
Combined state and effects
class CounterView extends StatelessWidget {
const CounterView({super.key});
@override
Widget build(BuildContext context) {
return BlocConsumerWithEffects<CounterCubit, CounterState, CounterEffect>(
onEffect: (context, bloc, effect) {
if (effect case ShowSnack(:final message)) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(message)),
);
}
},
builder: (context, bloc, state) {
return Text('${state.count}');
},
);
}
}
The bloc must mix in HasEffects<S, E>, while BlocEffectListener accepts
any EffectsSource<E>. The consumer hands the bloc to builder, listener
and onEffect, and its buildWhen and listenWhen get the previous and the
current state; BlocEffectListener.onEffect gets only (context, effect).
listener is optional; pass it, with an optional listenWhen, to react to
state changes as well. listenWhen without listener fails an assert.
Write all three type arguments: S and E are inferred from bloc: only in
code at language version 3.7 or later, which is the language version of your
app, not of this package.
Testing effects
Effects are not replayed, so subscribe before you trigger them.
expectLater subscribes at once:
test('signedIn sends GoHome', () async {
// Given
final cubit = AuthCubit();
addTearDown(cubit.close);
final effects = expectLater(cubit.effects, emits(isA<GoHome>()));
// When
cubit.signedIn();
// Then
await effects;
});
blocTest from bloc_test has no parameter for effects: call expectLater in
build: and await its future in verify:.
emitEffect is @visibleForTesting, so a widget test can send an effect
straight to the screen under test:
testWidgets('AuthPage shows the error', (tester) async {
// Given
final cubit = AuthCubit();
addTearDown(cubit.close);
await tester.pumpWidget(
MaterialApp(
home: BlocProvider.value(value: cubit, child: const AuthPage()),
),
);
// When
cubit.emitEffect(ShowError('Wrong password'));
await tester.pumpAndSettle();
// Then
expect(find.text('Wrong password'), findsOneWidget);
});
A widget test that mocks the cubit, with MockCubit from bloc_test or a
mocktail Mock, has to stub effects as well: unstubbed, it returns null,
and the listener fails with a TypeError. Stub it with a stream that the test
controls:
class MockAuthCubit extends MockCubit<AuthState> implements AuthCubit {}
testWidgets('AuthPage shows the error of a mocked cubit', (tester) async {
// Given
final cubit = MockAuthCubit();
final effects = StreamController<AuthEffect>.broadcast();
addTearDown(effects.close);
when(() => cubit.state).thenReturn(const AuthState());
when(() => cubit.effects).thenAnswer((_) => effects.stream);
await tester.pumpWidget(
MaterialApp(
home: BlocProvider<AuthCubit>.value(value: cubit, child: const AuthPage()),
),
);
// When
effects.add(ShowError('Wrong password'));
await tester.pumpAndSettle();
// Then
expect(find.text('Wrong password'), findsOneWidget);
});
An effect that nothing listens to is lost, for example one emitted from the
bloc's constructor or before the listening widget is mounted. Set
debugPrintUnheardEffects = true, for example at the start of main(), to get
a warning about it in debug builds. It is off by default, because tests of a
bloc often emit effects that they do not listen to.
Conditions
whenChanged returns a BlocCondition<S>, a plain
bool Function(previous, current). It goes straight into buildWhen,
listenWhen and selectorShouldRebuild, in this package's widgets and in
BlocBuilder, BlocListener and BlocConsumer. When the widget has its type
arguments, whenChanged needs none: the state type comes from the widget, so a
typo in the selector is a compile error. When the widget infers them from
bloc:, or the condition is stored in a variable, type the selector's
parameter: whenChanged((CounterState state) => state.count).
BlocBuilder<CounterCubit, CounterState>(
buildWhen: whenChanged((state) => state.count),
builder: (context, state) => Text('${state.count}'),
)
// Several fields: select a record of scalars.
buildWhen: whenChanged((state) => (state.count, state.label)),
// Collections: pass equals.
listenWhen: whenChanged((state) => state.items, equals: listEquals),
// Anything else: a plain predicate.
buildWhen: (previous, current) =>
previous.count != current.count && current.count.isEven,
Select scalars or records of scalars. A list, also inside a record, is
compared by identity, so pass equals: (for example listEquals from
package:flutter/foundation.dart). Never build a new list in the selector,
such as (state) => [state.a]: it is then always "changed".
RebuildPolicy, ListenPolicy and their factories are deprecated and will
be removed in 1.0:
| Deprecated | Replacement |
|---|---|
onChange(sel).shouldRebuild, onChangeListen(sel).shouldListen |
whenChanged(sel) |
onChangeBy(sel, equals: eq), onChangeListenBy(sel, equals: eq) |
whenChanged(sel, equals: eq) |
whenRebuild(p).shouldRebuild, whenListen(p).shouldListen |
p |
always(), alwaysListen() |
omit buildWhen / listenWhen |
never(), neverListen() |
(previous, current) => false |
distinct(), distinctListen() |
(previous, current) => previous != current (omitting the parameter differs only for a first emitted state equal to the initial one, which emit does not skip) |
a.and(b), a.or(b), a.not() |
(p, c) => a(p, c) && b(p, c), ||, ! |
Where the widget has no type arguments, keep them on the condition:
onChange<S, T>(sel).shouldRebuild becomes whenChanged<S, T>(sel).
Until 1.0, whenListen clashes with whenListen from bloc_test. In tests
that import both, hide this package's one:
import 'package:bloc_plus/bloc_plus.dart' hide whenListen;
UI widgets with bloc in callback
class CounterView extends StatelessWidget {
const CounterView({super.key});
@override
Widget build(BuildContext context) {
return BlocBuilderWithBloc<CounterCubit, int>(
builder: (context, bloc, state) {
return Text('$state');
},
);
}
}
The *WithBloc widgets accept any StateStreamable<S>, like flutter_bloc's,
and BlocListenerWithBloc has a const constructor.
BlocBuilderWithBloc, BlocListenerWithBloc, BlocConsumerWithBloc and
BlocConsumerWithEffects resolve the bloc once per build, so after bloc:
changes from an instance to null they follow the provided bloc at once, and
their callbacks get the bloc whose states they receive. BlocBuilder and
BlocListener keep the previous instance instead.
Optional lookups
import 'package:flutter_bloc/flutter_bloc.dart'; // exports read, watch, select
void tryIncrement(BuildContext context) {
final counterCubit = context.read<CounterCubit?>();
counterCubit?.increment();
}
With a nullable type argument, the lookups of provider handle a bloc that
may not be provided: context.read<CounterCubit?>(),
context.watch<CounterCubit?>() and
context.select<CounterCubit?, int?>((cubit) => cubit?.state.count) return
null when there is no provider of CounterCubit, rethrow any other error,
such as one thrown while the provider creates the bloc, and rebuild as their
non-null forms do. They replace the deprecated readOrNull, watchOrNull
and selectOrNull, which return the same values, while
context.withBloc((CounterCubit cubit) => cubit.state.count) becomes
context.read<CounterCubit?>()?.state.count. When the selector is not a
member chain, write cubit == null ? null : ...; when the callback of
withBloc does more than one member access, use a local variable and a null
check. bloc_plus does not export these lookups: a file without the
flutter_bloc import needs it, and drops the bloc_plus import when it used
bloc_plus only for the helpers.
Compared with
Measured against the versions that were current in November 2026.
bloc_presentation(1.1.2+3) adds one-off events to a bloc in the same way: a broadcast stream in a mixin, and a listener that composes insideMultiBlocListener. HereemitEffectafterclose()does nothing, whereemitPresentationthrows aStateError;BlocEffectListenerfollows the provided bloc afterbloc:changes from an instance tonull, whereBlocPresentationListenerkeeps the old instance;debugPrintUnheardEffectswarns about an effect that nothing listens to; andBlocConsumerWithEffectsbuilds, listens and handles effects for one bloc. For tests,bloc_presentation_testis a separate package; here the README has recipes.CancelableOperationfrompackage:async(2.13.1) gives nearly the same result asRestartableTaskwhen it is written withvalueOrCancellation()andonCancel: the superseded caller is released at once, its result and its later failure are dropped, and the failure of the latest run is rethrown. Only a value or a failure that arrived just before the cancel, while the caller had not resumed yet, is still delivered there, whereRestartableTaskdrops it. Awaiting itsvalueinstead never releases a superseded caller. In a Cubit it takes a field, a cancel, a wrap and aclose()override per task, whereRestartableTasksMixintakes a key and cancels every run atclose().bloc_concurrency(0.3.1) has event transformers for the handlers of aBloc:restartable(),droppable(),sequential()andconcurrent(). It has nothing for a Cubit, which has no events. Inside aBloc, prefer it toRestartableTasksMixin.
Versioning
These rules apply from 1.0.0. Until then a 0.x minor may contain breaking changes, as 0.4.0 does.
- The public API is everything exported from
package:bloc_plus/bloc_plus.dart. - A major version is needed to remove or rename an export, to change a
signature incompatibly, to change the superclass of a widget, to change
documented behaviour, or to add an annotation that makes the analyzer
report code that was clean before, such as
@protected.@Deprecatedis the exception (next item). - A minor version may add exports, members and optional parameters, support a new major of a dependency when no signature here changes, raise the minimum Dart or Flutter SDK or the minimum version of a dependency (a new major included), and deprecate an API, which is then removed no earlier than the next major.
- Not covered: classes that
implementthis package's classes or mixins by hand (EffectsSourceis the exception, and gets no new member without a major), subclasses of its classes and widgets, and overriding members of its widgets. Applying the mixins withwith, or building on them withmixin X on ..., is covered. Where a widget is documented as aflutter_blocwidget with something added, the rest of its behaviour is that of theflutter_blocversion the app resolves; a change there is not a change of this package. - A member added in a minor may clash with a member of the same name in a class that applies a mixin or extends a class of this package, and an optional parameter added to a method of a mixin breaks an override of that method. Neither is treated as a breaking change.
Libraries
- bloc_plus
- Async helpers for Cubits, one-off effects, conditions and widgets for
flutter_bloc.