BlocProvider doesn't pass Bloc to PopUp

Viewed 44

Why doesn't my BlocProvider provides the bloc into a PopUp. The bloc was registered as factory in the di, it would work if I registered the Bloc as a singleton, and wrap the PopUp with a second BlocProvider.

Is it bad to have multiple BlocProviders providing the same bloc respectively having the Bloc registered as singleton and not as factory?

All answers and comments are highly appreciated. Thanks!

Code of the PopUp

class HelpPopUp extends StatelessWidget {

  const HelpPopUp({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text('Help'),
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          BlocProvider(
            create: (context) => sl<HelpBloc>(),
            child: BlocBuilder<HelpBloc, HelpState>(
              builder: (context, HelpState state) {
                return state.map(
                  (state) => Text(state.toString()),
                  loading: (state) {
                    return Text(state.toString());
                  },
                  submitted: (state) {
                    return Text(state.toString());
                  },
                  submitting: (state) {
                    return Text(state.toString());
                  },
                  failure: (state) {
                    return Text(state.toString());
                  },
                  unsubmitted: (state) {
                    return Text(state.toString());
                  },
                  helpRequestDataPulled: (state) {
                    return Text(state.helpRequestPull.toString());
                  },
                );
              },
            ),
          ),
          Text('Press the submit button to get psychological help.')
        ],
      ),
      actions: [SubmitButton()],
    );
  }
}

Code where PopUp is used

class HelpIcon extends StatelessWidget {
  const HelpIcon({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Padding(
        padding: EdgeInsets.only(left: 24, top: 16),
        child: GestureDetector(
            onTap: () {
              showDialog(
                  context: context,
                  builder: (BuildContext context) {
                    return HelpPopUp();
                  });
            },
            child: FaIcon(FontAwesomeIcons.handsHelping)));
  }
}

Code where BlocProvider is instantiated

 MultiBlocProvider(
                    providers: [
                      BlocProvider(
                        create: (BuildContext context) => sl<TimetableBloc>()
                          ..add(GetCachedTimetable(
                              oldTimetable: Timetable.empty())),
                      ),
                      BlocProvider(
                        create: (BuildContext context) => sl<AuthBloc>(),
                      ),
                      BlocProvider(
                        create: (BuildContext context) => sl<AbsenceBloc>(),
                      ),
                      BlocProvider(
                        create: (BuildContext context) => sl<SettingsBloc>(),
                      ),
                      BlocProvider(
                        create: (BuildContext context) => sl<HelpBloc>(),
                      )
                    ],
                    child: BlocBuilder<AuthBloc, AuthState>(
                      builder: (context, state) {
                        initFeatures(context);

                        return AuthWrapper();
                      },
                    ),
                  )
1 Answers

The only case where you won't have access to Bloc is when the context in which you call popUp is not in a sub-tree of the provider containing context. So doule check the way you declare BlocProvider.

You have an additional argument with BuildContext in the constructor appContext. This is an interesting thing that might cause some problems.

Could you show how you declare BlocProvider at the top level? And how do you call the popUp?

Related