Redirection changes the location to a new one based on application state. For example, redirection can be used to display a sign-in screen if the user is not logged in.
A redirect is a callback of the type GoRouterRedirect. To change incoming location based on some application state, add a callback to either the GoRouter or GoRoute constructor:
redirect: (BuildContext context, GoRouterState state) { if (AuthState.of(context).isSignedIn) { return '/signin'; } else { return null; } },
To display the intended route without redirecting, return null
or the original route path.
There are two types of redirection:
GoRouter
constructor. Called before any navigation event.GoRoute
constructor. Called when a navigation event is about to display the route.You can also redirect using Named routes.
redirectLimit
to configure the maximum number of redirects that are expected to occur in your app. By default, this value is set to 5. GoRouter will display the error screen if this redirect limit is exceeded (See the Error handling topic for more information on the error screen.)