4

Assume this scenario: page1 Navigator.push to page2. user on page2 clicks the back button, so page2 pops and page1 regains view. How do I catch this event on page1?

shaharsol
  • 991
  • 2
  • 10
  • 31

4 Answers4

6

You can check like this by passing parameter from Navigator.pop...

from the second screen:-

 Navigator.pop(context, 'updateList'),

and on the first screen check like this and pass condition which you want:-

FloatingActionButton(
                  onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (BuildContext context) =>
                            new ManageAssignments(),
                      ),
                    ).then((val) {
                      if (val == 'updateList') getAssignementList();
                    });
                  },
                  backgroundColor: Theme.of(context).primaryColor,
                  child: Icon(Icons.add),
                )

Hope it will work for you :-)

Zeeshan Ansari
  • 1,413
  • 12
  • 21
  • In Dart 2, new keyword is optional. Furthermore, using new is discouraged by the [Effective Dart guidelines](https://dart.dev/guides/language/effective-dart/usage#dont-use-new). – Ravinder Kumar Nov 14 '19 at 09:03
3

So when Navigator.push() happens it returns a Future. And on Navigator.pop() the future is resolved. You can return value from Navigator.pop like this Navigator.pop(context, "MyResult"); That result will be captured like this - final result = Navigator.push(). You can use the result in the widget from where Navigator.push() was called.

Braj
  • 588
  • 3
  • 15
2

Use WillPopScope widget in the pop screen, then to get notified when screen is popped use onWillPop function.

Like this:

return WillPopScope(
      child: Scaffold(
          body: new Container(), // your body content
      onWillPop: (){
      //Screen is poped.
    },
    );
    
)
Abed
  • 3,999
  • 1
  • 17
  • 28
Chetan Raval
  • 41
  • 1
  • 5
0

You can use provider package and send notification when pushing back and notify listeners and update what you want.

gtr Developer
  • 2,369
  • 16
  • 12