I have a button in page 1 with onpressed that does
Navigator.push(context, customroutebuilerforanimation(page2()));
In page2 I have :
class Page2 extends StatefulWidget {
const Page2({super.key});
@override
State<Page2> createState() => _Page2State();
}
class _Page2State extends State<Page2> {
@override
void initState() {
super.initState();
context.read<mybloc>().add(myblocevent());
}
@override
Widget build(BuildContext context) {
return Container(
child: BlocConsumer<myblocBloc, myblocState>(
listener: (context, state) {
// TODO: implement listener
},
builder: (context, state) {
return Column(
children: [
if(state is something1) Placeholder(),
if(state is something2) SizedBox.shrink(),
],
);
},
));
}
}
Now when I open up the application and/or restart it and navigate to page2, the mybloc event does not get fired for some reason for the first time. please assume there is an APPBAR there that I missed to represent, that allows me to go back (navigator.pop())
and then come back to page2. When I do that, the event fires and results me the states that are not initial.
What can be the cause of this? I have tried looking hard, couldn't find the solution yet.
I am using GetIt, so the bloc is initialized and is provided in main, so no issues there.
Tried some work arounds , like the addpostframe callback method , did not work.
Tried to put it inside of didchangedependencies()
method, did not work.
Tried to add it as a function in the page1 button's on pressed instead of the init of page 2, did not work.
I have one last potential solution but it is a work around that I do not want to implement, since its more of a band-aid solution and not an actual solution to an issue.
I am expecting it to fire as soon as I enter the page regardless how or when application started.
Thank you for your responses in advance.
postframecallback
.mybloc
(both GetIt and BlocProvider)?