skip to Main Content

When looking for on how to clear all routes and replace with a new one, all the solutions propose something like this

  void clearStackAndReplace(String path, Object? extra) {
    while (canPop() == true) {
      pop();
    }
    pushReplacement(path, extra: extra);
  }

But pushReplacement is preseving the state of the new route, and I don’t want this, since I have some code on the initState method of the new route that I want to run.

How can I do this?

2

Answers


  1. You can write a function to pop every route, and push a new one when it’s empty, something likw this:

    void clearAndNavigate(String path) {
      while (getGoRouter().canPop() == true) {
      getGoRouter().pop();
    }
      getGoRouter().pushReplacement(path);
    }
    

    getGoRouter is an instance of GoRouter

    Login or Signup to reply.
  2. What about Navigator.popUntil(context, ModalRoute.withName('/login'));?

    here the reference

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