简体   繁体   中英

How to reload a page on Navigation.pop in Flutter?

I want to have page A reload, re-run the Http requests, once I pop back to it from another page that lets you edit the data page A displays. The other page is triggered by a long press in a ListView generated by a function within page A. IE: Page A:

_PageAState

  • Reload function I need to call

Scaffold

  • List item

  • Text

  • ETC...
  • Expanded
    • ListView
      • Gesture Detector that navigates
  • ETC

How can I trigger a function of any sort or make the page reload when I pop back to it?

I've tried passing data from the pop and using an if statement but the nested structure messes it up.

of course, there's a way to do that! As you realized, the first route needs to get notified when the pushed Route gets popped.

Thankfully, there's a tutorial in the Flutter documentation that handles returning data from Route s. In your case, no data gets returned but you can still use the same principle to wait for the route to be popped: Simply await the Navigator.push call! This will return once the other route gets popped.

await Navigator.push(
  context,
  MaterialPageRoute(builder: (context) => OtherPage()),
);
// TODO: Now, refetch the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM