How can i do push replacement using go router plugin in flutter [go router]

Viewed 1899

I am using the go router plugin for my flutter web application, but I didn't find any way to do push replacement in navigation.

not only push replacement, can we do any other type of navigation link

  • pushNamedAndRemoveUntil
  • popUntil because these navigation options must be needed in kind of any system!

What I tried

  • context.go(userStorage.redirectURL!);
  • GoRouter.of(context).go(PagesCollection.adminDashboard);

they only pushed the next page, not replacing

Note: I want this functionality in go router

3 Answers

Try this

Router.neglect(context, () {
            context
                .goNamed('third', params: {"id": ID});
          });

It will neglect your current page. Hop this will helps you

You can do it in a way ->

context.pop();
context.push(routeName);

Adding both these commands works similar to pushReplacement.

What you can do is use Sub-Route Redirects in Go Router.

Let's consider you want users to first go on your Homepage and then from there they tap Login, and go to Login page and then after Logging in, they go to Dashboard Page.

But when they press Browser's Back Button they should go to Homepage and not the LoginPage i.e skipping the LoginPage just like "pushNameReplacement".

Then for this to happen you can configure redirects of each LoginPage and Dashboard page and get this functionality.

You can configure it such that whenever user (including from Browser's History) goes to Dashboard link it first checks if its Logged In then it opens otherwise it displays Login Page automatically.

Related