0

I wanted to add event when initializing bloc inside the 'main.dart'. But It didn't call init event . Are there any way to do this without calling inside 'initState' of the next class

void main() {
  runApp(
    MultiBlocProvider(providers: [
      BlocProvider(create: (context) => CountlyBloc()..add(CountlyInitEvent()))
    ], child: MyApp()),
  );
}

3 Answers 3

1

BHARATH T 's answer is almost correct but you have to set lazy to false not true. Since true means you DO want it to be created lazy. The rest is correct

0

Just set lazy param inside the BlocProvider as true. By default, the bloc is instantiated only when it is used first. Setting the lazy param to true, forces it to instantiate at the moment.

void main() {
  runApp(
    MultiBlocProvider(providers: [
      BlocProvider(create: (context) => CountlyBloc()..add(CountlyInitEvent()),lazy:true,)
    ], child: MyApp()),
  );
}

Hope it helps! Happy coding:)

0

MultiBlocProvider( providers: [ BlocProvider(create: (context) => CountlyBloc()..add(CountlyInitEvent()),lazy:false,) ], child: MyApp()),

in order to initialize the bloc instance immediately

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.