Home:ALL Converter>Bloc to Bloc Communication Mutibloc Provider

Bloc to Bloc Communication Mutibloc Provider

Ask Time:2020-04-29T04:26:21         Author:user3836415

Json Formatter

I'm using flutter_bloc and I need a bloc to communicate with another bloc - I have an activityBloc that needs to listen to the authBloc to determine if the author is authenticated and get the userid ( the intention is to start listening to document changes on firestore based on the userid from authentication).

I'm passing in the dependant object to the activityBloc via the constructor.

class ActivityBloc extends Bloc<ActivityEvent, ActivityState> {
  final AuthBloc authBloc;
  final ActivityRepository repo;
  StreamSubscription authSubscription;
  StreamSubscription activitySubscription;
  String id; // userid
  int limit = 10;

  ActivityBloc({this.authBloc, this.repo}) {
    id = "";

    authSubscription = authBloc.listen((state) {
      if (state is AuthenticatedState) {
        id = state.user.uid;
        ...
          });
        });
      }
    });
  }

  @override
  ActivityState get initialState => ActivityInitial();
...
}

I need to have these exposed within the a multibloc provider, how would I be able to instantiate the blocs within the mulitbloc provider where one bloc needs to be passed into another bloc ?

Thanks

Author:user3836415,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/61489389/bloc-to-bloc-communication-mutibloc-provider
Chinmay Mourya :

You can use the instance of one bloc into another bloc by following.\nreturn MultiBlocProvider(\n providers: [\n BlocProvider<AuthBloc>(\n create: (BuildContext context) => AuthBloc(),\n ),\n BlocProvider<ActivityBloc>(\n create: (BuildContext context) => ActivityBloc(\n authBloc: BlocProvider.of<AuthBloc>(context),\n repo: /// instance of your activity repository,\n ),\n ),\n ],\n child: App(),\n);\n",
2020-11-02T15:59:48
yy