0

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.

RaSha
  • 1,356
  • 1
  • 16
  • 30
Nefarious
  • 458
  • 6
  • 21

1 Answers1

0

The BlocBuilder<RegionBloc, RegionState> will only react to RegionState.

You need to create a BlocBuilder<NeighborBloc, NeighborState> to react to NeighborSelected

F Perroch
  • 1,988
  • 2
  • 11
  • 23
  • I copied the print from the RegionBloc where it looks at the two bloc's to be sure they aren't null and they are the instances of the classes. I edited the question to add this info. – Nefarious Nov 07 '20 at 22:56
  • Do you create those bloc somewhere else before ? IMO, the instances you get are not those you creates above, so you may get a wrong behavior – F Perroch Nov 07 '20 at 23:01
  • They are not created any other place. I will look to see if when a dart instance is created, if there is a unique identifier and see if I can use it to tell what you claim. I am working to figure out how to create them else where and pass them in. – Nefarious Nov 07 '20 at 23:06
  • I print out the hashCode for the two bloc's in the regionBloc constructor and on each of the two bloc constructors and they are the same. – Nefarious Nov 07 '20 at 23:15
  • Sorry I'm wrong and both `BlocProvider.of(context)` and `BlocProvider.of(context)` references the instances above. Providers just need to be created in order. – F Perroch Nov 07 '20 at 23:16
  • I have understand you problem and edited the answer ;) – F Perroch Nov 07 '20 at 23:22
  • Unfortunately there is an instance of NeighborBloc in a different widget that responds to the NeighborSelect event in the NeighborEvent class. There was a name collision, im not that fond of dart, in the name of the event that the RegionBloc was adding and I think it confused the bloc subsystem. When the event name added by RegionBloc -> NeighborSelected is changed to RegionNeighborSelected the event is actually added. – Nefarious Nov 07 '20 at 23:36
  • RegionBloc can only add RegionEvent instances. – Nefarious Nov 07 '20 at 23:37
  • Do the `NeighborSelected` is part of `RegionBloc` ? – F Perroch Nov 07 '20 at 23:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/224265/discussion-between-f-perroch-and-nefarious). – F Perroch Nov 07 '20 at 23:47