skip to Main Content

In flutter, I’m on one Widget and there is logout functionality. So once I click on it I want to remove all other widget’s instance from the Widget tree and jump on logout widgets, so what I need to use?

TextButton.icon(
  label: const Text('Logout'),
  icon: const Icon(Icons.logout),
  onPressed: () {
    Provider.of<Auth>(context, listen: false).logout();
    Navigator.pushAndRemoveUntil(
      context,
      ModalRoute.withName(AuthScreens.routeName) as Route<Object>,
      (route) => false,
    );
  },
)

2

Answers


  1. Chosen as BEST ANSWER

    So I was using wrong approached there. Basically, I was ignoring the it's parameter's Type value, which should be only Route to String.

    Navigator.pushAndRemoveUntil(
                  context,
                  MaterialPageRoute(builder: (context) => ProductListingWidget()),
                  (route) => false,
                );
    

    with this changes I solved the issue, You can compared my previous code and this code, you will find the proper different in it.


  2. You may have to use popUntil

    Navigator.popUntil(context, ModalRoute.withName(AuthScreens.routeName))
    

    Or if you are popping and pushing to new page then

    Navigator.pushAndRemoveUntil<dynamic>(context, MaterialPageRoute<dynamic>(builder: (context)=> NewPage()), ModalRoute.withName(AuthScreens.routeName));
    

    Here, NewPage represents the page which user to be navigated after logout.(E.g: RegistrationPage())

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search