I want an event to be sent to the bloc when the page loads. The problem is that the event is not sent to the bloc. What could be the problem?
I’ve done this many times with no problems. I can’t understand what’s wrong now.
class Page extends StatelessWidget {
static PageRoute route() {
return FadePageRoute(page: Page());
}
const Page({super.key});
@override
Widget build(BuildContext context) {
return BlocProvider<Bloc>(
create: (context) => NewsBloc(GetIt.instance<Service>())
..add(Event(189)),
child: _PageFlow());
}
}
2
Answers
When use bloc, you must seperate between create bloc and send event bloc.
Below code is for create bloc.
If you want to send event to bloc, do inside the _PageFlow(). Example below i replace _PageFlow() with GestureDetector
Check to see if your UI has a widget that listens to Bloc’s state changes. Check to see if you use
BlocBuilder
orBlocListener
orBlocConsumer
orcontext.watch
orcontext.select
to make changes or build interfaces based on state or not.Because a
Bloc
is created lazily by default, when has event added and state listener, bloc will created. If you want to create aBloc
immediately, you can setlazy
inBlocProvider
totrue
.Hope my explanation will help in your problem. Happy coding 🙌