Here's my Flutter app MultiBlocProvider setup;
MultiBlocProvider(
providers: [
BlocProvider<LocationBloc>(create: (BuildContext context) {
return LocationBloc();
}),
BlocProvider<AddressBloc>(create: (BuildContext context) {
return AddressBloc(
location: BlocProvider.of<LocationBloc>(context)
..add(LocationStarted()))
..add(AddressStarted());
}),
BlocProvider<CampaignBloc>(
create: (BuildContext context) => CampaignBloc(
addressBloc: BlocProvider.of<AddressBloc>(context),
repo: CampaignsRepository())
..add(CampaignsInitial()),
),
BlocProvider<BusinessBloc>(
create: (BuildContext context) => BusinessBloc(
addressBloc: BlocProvider.of<AddressBloc>(context),
repo: BusinessRepository())
..add(BusinessInitial()),
)
],
child: MaterialApp(...)
,
)
LocationBloc gets the location from location services. AddressBloc gets an updated location from the LocationBloc and converts it to an address.
CampaignBloc listens to the AddressBloc stream in it's constructor for address(location) changes and emits the campaigns for the changed location.
CampaignBloc({required this.repo, required this.addressBloc})
: super(CampaignState()) {
_addressSubscription =
addressBloc.stream.asBroadcastStream().listen((AddressState state) {
if (state is AddressLoadSuccess) {
Location location = state.location;
add(CampaignChangedLocation(location: location));
}
});
}
BusinessBloc does the same thing in it's constructor and (should) emit businesses for the changed location.
BusinessBloc({required this.repo, required this.addressBloc})
: super(BusinessInitial()) {
_addressSubscription =
addressBloc.stream.asBroadcastStream().listen((AddressState state) {
if (state is AddressLoadSuccess) {
Location location = state.location;
add(BusinessChangedLocation(location: location));
}
});
}
HomeView has a BlocBuilder<CampaignBloc, CampaignState> that builds the list of campaigns.
AlliesView has a BlocBuilder<BusinessBloc, BusinessState> that builds the list of businesses.
The CampaignBloc is receiving the updated location when building the HomeView but on transition to the AlliesView the listener on the AddressBloc stream is not receiving the updated location because it has subscribed to the stream after the event. How can I get the updated location in subsequent listeners to the AddressBloc stream?