How to push replacement removing whole navigation stack in flutter?

Viewed 10676

I have got my homepage from which I can go to profile page in my flutter application. On profile icon pressed, I push my context to profile page where I have a button to change password. When I press the change password, it opens an alert dialog which has textfield to enter password. I have a function logout which logs out of the application by clearing preferences and pushing context to login screen as replacement. Now I want to logout of my application when I change the password. But it pushes replacement to the top of the navigation stack which returns login page within the stack. How do I resolve this issue?

_changePassword() async{
    print(widget.id);
    var data = {
      "newPassword": _passcontroller.text,
      "confirmNewPassword": _confirmpasscontroller.text
    };
    var res = await CallApi().putData(data, 'users/${widget.id}/password/change');
    var response = res.data;
    print(response);
    if (response['success']) {
      showSimpleFlushbar(context, 'Password Changed Successfully.');
      logout(context);
    }
  }
2 Answers

Use this:

It removes previous all navigations and brings you to landing page(first page).

()=> Navigator.pushAndRemoveUntil(
                  context,
                  MaterialPageRoute(builder: (BuildContext context) => LoginPage()),
                  ModalRoute.withName('/'),
                )

OR

it will remove previous whole navigations.

//Use this for logout

 Navigator.pushAndRemoveUntil(
                  context,
                  MaterialPageRoute(builder: (BuildContext context) => LoginPage()),
                 (Route<dynamic> route) => false
                );
//OR

 Navigator.of(context).pushNamedAndRemoveUntil('/landingpage', (Route<dynamic> route) => false);

Use this code for removing whole navigation stack in flutter

Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context) =>
                    ClassName()), (Route<dynamic> route) => false);
Related