James Hancock
James Hancock

Reputation: 3547

Flutter Navigation 2 - Reload data on activate

Say I have page 1, and page 2, and both are loaded using Router Delegate and Route Information parser. If you go back up the stack with the back button or click a link that goes to the same route, you get the old page, which is fine as far as it goes, but the problem is that almost always what you really want is to reload data with any changes.

How does one hook into the navigation with the page itself so that when it becomes the active page, it can kick off a reload of data from a server?

Upvotes: 1

Views: 293

Answers (1)

Thomas Alfonso
Thomas Alfonso

Reputation: 41

what I do to solve this problem is define a callback for the route. For example:

onTap: () async {
  var response = await Navigator.push(
      context,
      MaterialPageRoute(
          builder: (context) => Page2()));
  if (response == "true") {
      print("hello");
  }
  getData();
},

What we are doing there is adding a new route to the stack and waiting for a response. Once you have an answer (because it makes a pop) continue the execution and call the void getData ()

in fact so you can even return values, for example:

 onPressed: () {
     Navigator.pop(context, 'response');
 },

Upvotes: 1

Related Questions