Bloc: Initiate new event of different bloc at transition

Created on 7 Jan 2019  路  6Comments  路  Source: felangel/bloc

Is your feature request related to a problem? Please describe.

I wish to listen to event like startLocationService and than init a new event/s (like log)

Describe the solution you'd like

I was thinking of mingling events at the BlockSupervisor. But given that, I just lately started with dart and flutter so I am not sure of anything.

question

All 6 comments

@mbn18 I would recommend injecting the stream of location events into your other bloc. That way you can listen for new events and dispatch whatever events you want in the other bloc.

onTransition was intended to be used for logging state changes and I don鈥檛 think you need to involve BlocSupervisor for this use case.

Let me know if that helps. I can try to put together an example app of how to do this if you鈥檇 like, just let me know. 馃憤

Thanks for the idea, I think can do it.
Tough it might be a good idea to add to the docs instructions of how to bridge between blocs

@mbn18 Great suggestion, I'll update the docs with an section for communication between blocs.

Hi @felangel, Thank you for this awesome library.

Do you have an example on communicating between blocs? I have a case where I want to read application setting from a Json file.

Currently I am reading the json file and loading content into a static variable inside the main.dart widget. example below.

I am wondering if it will be a good solution to create another bloc called ApplicationSettingBloc and inject AuthenticationBloc into it and call appSettingService.loadAppSettings(); when AuthenticationUninitialized state is fired.

Do you think this is a overkill? Also, how do you pass streams of event to another bloc? Is it as simple as authenticationBloc.state? and in other bloc listen for the events? authenticationBloc.state.listen?

@override
  Widget build(BuildContext context) {
    return BlocProvider<AuthenticationBloc>(
      bloc: authenticationBloc,
      child: MaterialApp(
        theme: _kTheme,
        home: BlocBuilder<AuthenticationEvent, AuthenticationState>(
          bloc: authenticationBloc,
          builder: (BuildContext context, AuthenticationState state) {
            if (state is AuthenticationUninitialized) {

              appSettingService.loadAppSettings(); 
              return SplashPage();
            }
            if (state is AuthenticationAuthenticated) {
              return HomePage(); // 
            }
            if (state is AuthenticationUnauthenticated) {
              return LoginPage(userRepository: userRepository);
            }
            if (state is AuthenticationLoading) {
              return LoadingIndicator();
            }
          },
        ),
      ),
    );
  }

@john1452 thanks for the positive feedback!

Regarding your question, in my opinion there's nothing wrong with your ApplicationSettingsBloc having a dependency on an ApplicationSettingsService and calling load.

You can access the streams of state for a given bloc using Bloc.state and listen for changes by using Bloc.state.listen (or BlocBuilder in your Flutter UI).

I think the closest example I currently have in the docs is in the Login Tutorial where the LoginBloc has a dependency on the AuthenticationBloc and calls dispatch on the AuthenticationBloc for certain events. Take a look here on line 37.

Does that help?

Thanks @felangel I had a look at the login tutorial. The examples in the login tutorial solved my use case.

Thanks again for quick response.

Was this page helpful?
0 / 5 - 0 ratings