I have two BLoCs and need to listen for state changes from one in order to add events to the other and build the UI.
I think it's easier to explain in code:
@override
Widget build(BuildContext context) {
return BlocProvider<BlocA>(
create: (context) => BlocA(), <------ Create BlocA
child: BlocProvider<BlocB>(
create: (context) => BlocB(), <---- Create BlocB
child: BlocListener<BlocA, StateA>(
listener: (context, state) {
if (state is StateA) {
context.read<BlocB>().add(EventB()); <--- When BlocA is ready, add EventB to BlocB
}
},
child: BlocBuilder<BlocB, StateB>(
builder: (context, state) {
if (state is StateBInitial) {
return Container(color: Colors.yellow); <-- shows initial as expected
}
if (state is StateBSuccess) {
return Container(color: Colors.green); <-- success is never triggered
}
return Container(color: Colors.red);
),
),
),
);
}
In the example above I create 2 BLoCs, BlocA and BlocB. I add them in providers and then create a BlocListener that listen for BlocA changes and add events to BlocB.
The problem is that the BlocBuilder never triggers after the initial state. I can see in debugging that the Event added by the the BlocListener is registered and data is fetched and yielded, but the BlocBuilder never triggers even though the state has changed.
It is as if the BlocListener is calling a different BlocB.