The purpose of the go_router for Flutter is to use declarative routes to reduce complexity, regardless of the platform you're targeting (mobile, web, desktop), handling deep linking from Android, iOS and the web while still allowing an easy-to-use developer experience.
// unnamed routes
redirect: (state) {
// capture deep link
final from = state.queryParams['from'] ?? state.subloc;
// check for the app to be ready
final initialized = appState.initialized;
final initializing = state.subloc == '/init';
if (!initialized && !initializing) return '/init?from=$from';
// check for the user to be logged in
final loggedIn = loginInfo.loggedIn;
final loggingIn = state.subloc == '/login';
if (!loggedIn && !loggingIn) return '/login?from=$from';
if (loggedIn && loggingIn) return '/';
// strip the deep link
if( state.queryParams.contains('from') ) return state.subloc;
// no redirect
return null;
}
// named routes
redirect: (state) {
// capture deep link
final from = state.queryParams['from'] ?? state.subloc;
// check for the app to be ready
final initialized = appState.initialized;
final initializing = state.subloc == state.namedLocation('init');
if (!initialized && !initializing) {
return state.namedLocation('init', queryParams: {'from': from);
}
// check for the user to be logged in
final loggedIn = loginInfo.loggedIn;
final loggingIn = state.subloc == state.namedLocation('login');
if (!loggedIn && !loggingIn) {
return state.namedLocation('login', queryParams: {'from': from});
}
if (loggedIn && loggingIn) return state.namedLocation('home');
// strip the deep link
if( state.queryParams.contains('from') ) return state.subloc;
// no redirect
return null;
}
e.g.