skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    just wrap the Scaffold with WillPopScope

    WillPopScope(
      onWillPop: () async {
        SystemNavigator.pop();
        return false;
      },
      child:...
    }
    

  2. Wrap your Page’s Scaffhold with WillPopScope Widget and add this invokeMethod to onWillPop.

    return WillPopScope(
          onWillPop: () async {
            await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
            return false;
          },
          child: Scaffold(...),
       );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search