Sub topic 12
Controlling Concurrency with `bloc_concurrency` Transformers
This chapter assumes Dart async/await, Flutter widgets, and basic Bloc or Cubit usage. It focuses on event-driven Bloc classes. A concurrency transformer controls what happens when several events arrive before an earlier event finishes.
Why event order matters
An event handler can take time because it waits for a network request, database operation, or file read. If users create several events quickly, handlers may overlap.
For example, a search field can emit these events:
fflfluflut
If the request for f finishes after the request for flut, old results could replace newer results. The default event behavior allows handlers to run concurrently, so you must choose a policy when order matters.
| Transformer | Behavior | Useful for |
|---|---|---|
| `restartable()` | Cancel the previous handler when a new event arrives | Search-as-you-type |
| `droppable()` | Ignore new events while one handler runs | Preventing duplicate submissions |
| `sequential()` | Queue events and handle them one at a time | Ordered writes |
| `concurrent()` | Allow handlers to overlap | Independent work |
Use `restartable` for changing input
restartable() keeps the newest event. The older handler is cancelled from Bloc's event-processing stream, so its result cannot update the Bloc after a newer event takes over.
import 'package:bloc_concurrency/bloc_concurrency.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
sealed class SearchEvent {
const SearchEvent();
}
final class SearchQueryChanged extends SearchEvent {
const SearchQueryChanged(this.query);
final String query;
}
class SearchBloc extends Bloc<SearchEvent, List<String>> {
SearchBloc(this._search) : super(const []) {
on<SearchQueryChanged>(
_onQueryChanged,
transformer: restartable(),
);
}
final Future<List<String>> Function(String query) _search;
Future<void> _onQueryChanged(
SearchQueryChanged event,
Emitter<List<String>> emit,
) async {
final results = await _search(event.query);
emit(results);
}
}The request itself might continue inside a client that does not support cancellation. The important guarantee is that an obsolete handler cannot emit its result through this Bloc after it has been replaced.
Use `droppable` for repeated commands
droppable() ignores events that arrive while the current handler is running. This fits a save or submit button when a second tap should not create another request.
on<SavePressed>(
_onSavePressed,
transformer: droppable(),
);Use sequential() instead when every event must be processed, but in order. For example, a queue of local edits should not lose events. Use concurrent() when events do not affect one another.
Visualizing restartable behavior
Choose a transformer from the user action, not from a habit. Ask whether old work should be cancelled, ignored, queued, or allowed to overlap.
Exercise
Which transformer should you choose for a refresh button that must ignore taps while the current refresh is running?
Show answer
Use droppable(), because it ignores new refresh events until the active handler finishes.