Search code examples
flutterblocflutter-bloc

How can I get parameter from Bloc without using Blocprovider/Bloccomsumer...?


I have defined a bloc named "AppBloc" that manage user login/logout activity of user:

class AppBloc extends Bloc<AppEvent, AppState> {
  AppBloc()
      : super(
          const AppStateLoggedOut(
            isLoading: false,
          ),
        ) {

    on<AppEventGoToRegistration>((event, emit) {
      emit(
        const AppStateIsInRegistrationView(
          isLoading: false,
        ),
      );
    });
  }
}

In that AppBloc, I want to define user variable: User user and want to get that variable from another screen by call AppBloc().User . But I can not do the constructor for AppBloc() .

try: Add variable: User user to constructor of AppBloc

UPDATE:


Solution

  • You can use BlocProvider.of to get the registered bloc in the context.

    BlocProvider.of<AppBloc>(context).state