Search code examples
flutterflutter-layoutflutter-navigation

How to remove first screen from backstack in flutter


I have a login page that needs to shown for the first time, but once we entered the correct credential we land on home screen. This is working fine but problem starts when we press the back button which navigates to Login Screen again? Can we remove the login page from the back stack?

I am using the below navigation.

 void _navigateToNextScreen(BuildContext context) {
    Navigator.of(context)
        .push(MaterialPageRoute(builder: (context) => HomeScreen()));
  }

Solution

  • pushReplacement Worked

    void _navigateToNextScreen(BuildContext context) {
        Navigator.of(context)
            .pushReplacement(MaterialPageRoute(builder: (context) => WidgetOne()));
      }