what is the right way to provide a bloc?

Issue

hi guys i m new in flutter and my question is what is the right way to provide a bloc?
i decided to provide theme in main file in named rout section but the problem is when i need a bloc inside of other bloc i cant provide it.
here is my main file routes section the problem is for example i wanna provide a AssetDevicesPageBloc bloc to assetPage bloc how can i do such a thing cuz i dont have access. am i dong providing blocs in wrong way ? for first time i wanted to provided all blocs in one place then use blockprovider.value for each widget but when i searched about it,i found that its wrong way of using resources.please help.thank you

 return MaterialApp(
  routes: {
    'loginPage': (context) => BlocProvider(
          child: LogInPage(),
          create: (BuildContext context) {
            return LoginBloc(
                userRepository: userRepository,
                authenticationBloc:
                    BlocProvider.of<AuthenticationBloc>(context));
          },
        ),
    'AssetDevicePage': (context) => MultiBlocProvider(
          child: AssetDevicesPage(),
          providers: [
            BlocProvider(
                create: (BuildContext context) => AssetDevicesPageBloc(
                    userRepository: userRepository,
                    dataBaseRepository: dataBaseRepository,
                    deviceRepository: deviceRepository)),
            BlocProvider(
                create: (BuildContext context) => DeviceViewSwitcherBloc(
                    dataBaseRepository, userRepository, deviceRepository)),
            BlocProvider(
                create: (BuildContext context) =>
                    LampBloc(deviceRepository, dataBaseRepository)),
          ],
        ),

    'AssetPage': (context) => MultiBlocProvider(
          providers: [
            BlocProvider(
              create: (BuildContext context) =>
                  CategoryBloc(userRepository, dataBaseRepository),
            ),
            BlocProvider(
              create: (BuildContext context) =>
                  AssetPageBloc(BlocProvider.of<CategoryBloc>(context)),
            ),
          ],
          child: AssetPage(),
        ),

  },

Solution

The issue is happening because the bloc is not available in the context from which you are calling it.

You need to initialise the AssetDevicesPageBloc in the very first widget above the materialApp widget.

For example in MyApp Widget create an instance of the AssetDevicesPageBloc .

AssetDevicesPageBloc assetDevicePageBloc = AssetDevicesPageBloc(
userRepository: userRepository,
dataBaseRepository: dataBaseRepository,
deviceRepository: deviceRepository)
);

Than wrap the MaterialApp widget with BlocProvider.value and provide the created bloc like this:

BlocProvider.value(
  value: assetDevicePageBloc,
  child: MaterialApp(),
);

Now you can access your block anywhere inside any screen using :

BlocProvider.of< AssetDevicesPageBloc >(context)

But do remember if you initialise the bloc you should close its instance in the dispose method of the widget in which you initialised it. So call

assetDevicePageBloc?.close();

in the dispose method of the widget you initialised assetDevicePageBloc.

For reference see the official documentation

Answered By – harpreet seera

Answer Checked By – Cary Denson (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.