11

I have a page which is calling multiple classes and different widgets and by clicking on it user can hop to any page.

Currently the issue is I want to refresh the original page everytime it is shown to user.

When it is freshly loaded (causing no issue for now).

When user comes back to it from some other page using back key. old data is shown on the page, which I am trying to get rid of.

I have tried using Navigator.push method (Observer class method) and tried to listen when user presses backkey.

but there are multiple pages serving unique requests and I don't want to link everyone with that first page. as while clicking Navigator.pop(context) method i'll have to pass some string.

Does any one know how can I refresh the page when users comes back to it using backkey.

Vicky Salunkhe
  • 9,869
  • 6
  • 42
  • 59

3 Answers3

15

Navigator.push returns a Future when the MaterialPageRoute passed into it is resolved. You could await that Future to know when some other view has popped back to it.

Example,

Navigator.of(context).push(MaterialPageRoute(
  builder: 
    (context) => NewView(),
  ),
).then((_) {
  // Call setState() here or handle this appropriately
});
Ajil O.
  • 6,562
  • 5
  • 40
  • 72
  • 2
    I have already mentioned in my question that I have tried this method, please read the question, the method is not feasible in my use case. – Vicky Salunkhe Jul 20 '19 at 07:54
7

Had the same issue, the following worked for me (below code is in FirstPage()):

Navigator.push( context, MaterialPageRoute( builder: (context) => SecondPage()), ).then((value) => setState(() {}));

After you pop back from SecondPage() to FirstPage() the "then" statement will run and refresh the page.

CoderUni
  • 5,474
  • 7
  • 26
  • 58
  • Although this is a possible solution, it would be better to use state management to update the widget. – CoderUni Nov 06 '20 at 04:58
1

NavigatorObserver can listen to route changes.

You need to create your implementation overriding didPop, didPush etc. Then you can pass it to navigatorObservers field of MaterialApp or Navigator

Mikhail Ponkin
  • 2,563
  • 2
  • 19
  • 19
  • I have tried using the NavigatorObserver Class, you can see that I have already mentioned in my question. But in my use case it is of no help. – Vicky Salunkhe Jul 19 '19 at 10:39