Flutter: capture the future of the 2nd page after PushReplacement

Viewed 16

I have some code like:

Future pushNamed = Navigator.of(context).push(MaterialPageRoute(
          builder: (context) => XYZPage(),
        ));
pushNamed.then((_) => setState(() {}));

In general, this works fine, except for a case: in the XYZPage(), when the user clicks a button, it does a pushReplacement to another page ABCPage(). When that happens, the code above already gets to pushNamed.then. What I want instead, is to intercept the future when page ABCPage() finishes and refresh the current page before XYZPage(). Is it possible? Thanks!

2 Answers

The Flutter push documentation says:

'Returns a Future that completes to the result value passed to pop when the pushed route is popped off the navigator.'

The Future is not the builder Future. Its' Future that finishes on page.pop().

So if you want to listen to the pop() of the page - this is possible, just as you did

If you want to respond to builder: (context) => XYZPage(), you should do it inside XYZPage.build()

I figured out a way by using RouteAware and DidPopNext().

Related