I am fairly new to Flutter and very new to using BLoc.
I have a situation where I need to have a single BLoc respond to state changes in two other BLoc's.
The BLoC's are set up as follows:
body: MultiBlocProvider(
providers: [
BlocProvider(
create: (context) => NeighborBloc(repository: pcRepository),
),
BlocProvider(
create: (context) => FamilyBloc(repository: pcRepository),
),
BlocProvider(
create: (context) => RegionBloc(
repository: repository,
neighborBloc: BlocProvider.of<NeighborBloc>(context),
familyBloc: BlocProvider.of<FamilyBloc>(context)),
),
],
The constructor for RegionBloc is:
RegionBloc(
{@required this.neighborBloc,
@required this.familyBloc,
@required this.repository})
: assert(repository != null),
super(RegionEmpty()) {
print("NB: " + neighborBloc .toString());
print("FB: " + familyBloc .toString());
_neighborBlocSubscription = neighborBloc.listen((neighborState) {
if (neighborState is NeighborSelected) {
Postcode postcode = (neighborState as NeighborSelected).postcode;
add(NeighborSelected(postcode));
}
});
_familyBlocSubscription = familyBloc.listen((familyState) {
if (familyState is FamilySelected) {
Postcode postcode = (familyState as FamilySelected).postcode;
add(FamilySelected(postcode));
}
});
}
The result of the two print statements are: NB: Instance of 'NeighborBloc' FB: Instance of 'FamilyBloc'
I have a BlocObserver setup and I get the transitions for the neighborState
onTransition NeighborBloc, transition: Transition { currentState: NeighborLoading, event:
SelectNeighbor, nextState: NeighborSelected }
onChange NeighborBloc, Change { currentState: NeighborLoading, nextState:
NeighborSelected }
The RegionBloc->NeighborSelection is never added. The BlocBuilder for the RegionBloc is:
return BlocBuilder<RegionBloc, RegionState>(builder: (context, state) {
if (state is RegionEmpty) {
return Text('Empty Region');
}
if (state is RegionLoaded) {
return Text('RegionsLoaded');
}
if (state is NeighborSelected) {
return Text('NeighborSelected');
}
if (state is FamilySelected) {
return Text('FamilySelected');
}
//return Center(
// child: CircularProgressIndicator(),
//);
setPolygon();
return Container(
I have read this link and I think I am doing it: Flutter listen Bloc state from Other Bloc
I can't tell what I am not doing or doing wrong.