Search code examples
flutterdartflutter-navigation

Why is initial route not the only route on the stack after building Navigator?


If an initial route is defined, Navigator builds the stack with two routes: the initial one and '/' route.

Check out the example on the link showing that HomePage as the initial route has a back button that returns to '/' route.

How to make an initial route the only route on the stack?


Solution

  • So when you use, switch, you return the null in your default, which sets the HomePage as the initialRoute, and let the machine know that, the initialRoute is the one, and nothing to be returned in the Stack, except HomePage. Hence, you don't get to see the back button anymore.

       switch (settings.name) {
          case HomePage.route:
            return MaterialPageRoute(builder: (_) => HomePage());
          case OverviewPage.route:
            return MaterialPageRoute(builder: (_) => OverviewPage());
          default:
            // this would do the job
            return null;
       }
    

    I have made changes in the link also, you can try that too. Try out this link, and let me know.