Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - How to get notified when the page I pushed was popped by back button?

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?

like image 482
shaharsol Avatar asked Nov 05 '25 05:11

shaharsol


2 Answers

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 :-)

like image 68
Zeeshan Ansari Avatar answered Nov 08 '25 05:11

Zeeshan Ansari


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.

like image 31
Braj Avatar answered Nov 08 '25 04:11

Braj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!