2

My main page consists of a ListView generated by a FutureBuilder. The second page is opened by a FAB on the first page and is used to add elements to the ListView on the first page. My problem is that I can't find a way to refresh page one after popping the second one automatically to show the added elements.

I've searched other threads with similar issues but there wasn't a definitive answer.

  • If you are using a single activity approach you can make a global context for a few fragments and bind it to activity. For example, the context could be ViewModel with LiveData. – UjinUkr Jan 18 '20 at 21:45
  • And my advice to you, don't use Activity results/broadcast receivers, it's ugly and very complicated for support. – UjinUkr Jan 18 '20 at 21:49

1 Answers1

2

What you can do is await the pop from the next page and once that happens, you can trigger a function which re-loads your data.

Create a function called pushRoute which pushes a route on the navigator stack and use this function when you want to navigate.

As this is an asynchronous function, you can chain a .then() in the end of it. Whatever you pass to .then() will happen after the Navigator.pop() is hit on the second page.

pushRoute() async {
  await Navigator.push(...);
}
...
...
//when you want to navigate to page 2, use pushRoute().
//inside the onTap of your FAB
onTap() {
  pushRoute().then(() { //this .then() will be triggered when the Navigator.pop() is hit on the second page.
    loadData();
  }); //put all your data loading code in this function. (You can use the same loadData in the initState to load the very first time.)
}
Yudhishthir Singh
  • 2,941
  • 2
  • 23
  • 42