Dispatch first bloc event or cubit method, on page start inside StatelessWidget


I have 10 buttons in main menu of my app and each of them contains BlocBuilder inside them.

So when I click on those buttons to open a new page, I want to dispatch the first event, but I don’t know how. I can change all classes to stateful widget and then call bloc.dispatch(event) inside initialState() function, but I would like to discover another way, and not sure whether it’s the best way


In order to trigger the first event/method call inside BlocBuilder I had to add bloc argument and give parameter provided my BlocProvider, only after this I managed to call my method.

BlocBuilder<MyCubit, MyState>(
      bloc: BlocProvider.of<MyCubit>(context)..myFunction(),
      builder: (BuildContext context, state) {
//Your code...

Answered By – SardorbekR

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *