Home > Back-end >  Why putting bloc providers in a separate file/class throws an error?
Why putting bloc providers in a separate file/class throws an error?

Time:01-15

I have this code:

MultiBlocProvider(
  providers: [
    BlocProvider(
      create: (context) => CubitExample(),
    ),
  ],
  child: MaterialApp(
    home: Home(),
  ),
);

It's working fine, I have many cubits/blocs so I decided to move the providers list to a separate class:

MultiBlocProvider(
  providers: BlocProviders.blocs(),
  child: MaterialApp(
    home: Home(),
  ),
);

// The class 
class BlocProviders {
  static List<BlocProvider> blocs() {
    return <BlocProvider>[
      BlocProvider(
        create: (context) =>
          ChannelsCategoriesCubit(ChannelsCategoriesRepository()),
      ),
    ];
  }
}

Now the usual bloc error is thrown:

ProviderNotFoundException (Error: Could not find the correct Provider<ExampleCubit> above this BlocBuilder<ExampleCubit, ExampleState> Widget

I don't know why the error is thrown when I try the second code, why the error is thrown?

CodePudding user response:

It is really strange why this is behaving this way!!, I tried all possible work arounds.

Finally found a way around to this by using getter. Replace your function with getter. This should do the work.


class Providers {
  static get getproviders => [
        ...            
  • Related