Bloc Concurrency: Advance Event Transformer
How to control and transform the incoming events in BLoC?

Search for a command to run...
How to control and transform the incoming events in BLoC?

No comments yet. Be the first to comment.
Lessons from Code, Content, and Consistency

Reliability basics: Outbox pattern + why it matters

Introducing Kafka/Redpanda + move to event-driven workflow

Connect Orders ↔ Inventory (first working service-to-service flow)

Inventory service + gRPC + proto contracts

class CounterBloc extends Bloc<CounterEvent, int> {
CounterBloc() : super(0) {
on<IncrementCounterEvent>(
(event, emit) async {
await Future<void>.delayed(const Duration(seconds: 1));
emit(state + 1);
},
);
on<DecrementCounterEvent>(
(event, emit) async {
await Future<void>.delayed(const Duration(seconds: 1));
emit(state - 1);
},
);
}
Let's go on to the CounterPage now. Consider the code below.
Future<void> tick() => Future<void>.delayed(Duration.zero);
class CounterHome extends StatelessWidget {
const CounterHome({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Counter")),
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
FloatingActionButton(
onPressed: () async {
context.read<CounterBloc>().add(IncrementCounterEvent());
await tick();
context.read<CounterBloc>().add(IncrementCounterEvent());
await tick();
context.read<CounterBloc>().add(IncrementCounterEvent());
await tick();
context.read<CounterBloc>().add(IncrementCounterEvent());
await tick();
context.read<CounterBloc>().add(IncrementCounterEvent());
await tick();
context.read<CounterBloc>().add(IncrementCounterEvent());
},
child: const Icon(Icons.add),
),
const SizedBox(width: 8),
FloatingActionButton(
onPressed: () async {},
child: const Icon(Icons.remove),
),
],
),
body: BlocBuilder<CounterBloc, int>(
builder: (context, state) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$state',
style: Theme.of(context).textTheme.headline4,
),
],
),
);
},
),
);
}
}


transformer parameter to the 'onCounterEvent>()' method.sequential() transformer to add the sequential() transformer to the event.class CounterBloc extends Bloc<CounterEvent, int> {
CounterBloc() : super(0) {
on<IncrementCounterEvent>(
(event, emit) async {
await Future<void>.delayed(const Duration(seconds: 1));
emit(state + 1);
},
transformer: sequential(),
);
on<DecrementCounterEvent>(
(event, emit) async {
await Future<void>.delayed(const Duration(seconds: 1));
emit(state - 1);
},
transformer: sequential(),
);
}

class CounterBloc extends Bloc<CounterEvent, int> {
CounterBloc() : super(0) {
on<CounterEvent>(
(event, emit) async {
await Future<void>.delayed(const Duration(seconds: 1));
if (event is IncrementCounterEvent) {
emit(state + 1);
} else if (event is DecrementCounterEvent) {
emit(state - 1);
}
},
transformer: sequential(),
);
}
CounterEvent. The rest of the logic stays unchanged.
bloc_concurrency package. There are other transformers too. If you want to learn about other transformer checkout it's github.
