I have two pages I am navigating between. When navigating back, I want the first page to reload the data/rebuild itself, but it doesn’t seem to be working. I read through the replied here but I still cannot get it to work. Here is what I have:
Navigation from first page to second page
InkWell(
onTap: () async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
AddChallengeUnitsPageWidget(),
),
).then((_) => setState(() {}));
},
child: Icon(
Icons.add_circle,
),
),
Then coming back from the second page after a function runs (I need the update from this function to update the previous page)
onPressed: () async {
//I need the updated information from this function to display on the previous page
await updateChallenge();
Navigator.pop(context);
//I have tried removing this setState call and it did not fix the proplem.
setState(() {});
},
Any idea why the state of the page is not being updated properly?
2
Answers
You should do it on the first screen, like so:
to return result from second page use
Navigator.pop(context, result);
on first page make sure to add await and use
WidgetsBinding.instance.addPostFrameCallback
ifsetState
does not workvar result = await Navigator.push( context, MaterialPageRoute( builder: (context) => AddChallengeUnitsPageWidget(), ), );
WidgetsBinding.instance.addPostFrameCallback((_) => setState((){}));