Search code examples
flutterdartflutter-navigation

Return data from a screen on Flutter, without using Navigator.pop


I have a DetailPage that must return something to the code that called it. The documentation Return data from a screen explains that you must call Navigator.pop(context, whateverYouMustReturn) on some button.

So far so good, but what happens if the user clicks on the back button on the AppBar instead?? How do I return something then??

PS I'm using Navigator 1.0, btw


Solution

  • Provide your own leading widget and override the callback.

          AppBar(
            leading: BackButton(
              onPressed: () {
                Navigator.of(context).pop(myData);
              },
            ),
          ),