Search code examples
flutterflutter-go-router

how to exit on press back button from a screen


I am using go_router and I want to exit my app from a subroute when the user presses the back button. for example, I have routes '/', '/a', '/b', '/a/1'. now I from the '/a/1' route I want to exit the app when the user presses the back button(based on some condition). please help me


Solution

  • Wrap your Page's Scaffold with WillPopScope Widget and add this invokeMethod to onWillPop.

    return WillPopScope(
          onWillPop: () async {
            await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
            return false;
          },
          child: Scaffold(...),
       );