Instead of navigating to a route based on the URL, a GoRoute can be given a unique name. To configure a named route, use the name
parameter:
GoRoute( name: 'song', path: 'songs/:songId', builder: /* ... */, ),
To navigate to a route using its name, call goNamed
:
TextButton( onPressed: () { context.goNamed('song', pathParameters: {'songId': 123}); }, child: const Text('Go to song 2'), ),
Alternatively, you can look up the location for a name using namedLocation
:
TextButton( onPressed: () { final String location = context.namedLocation('song', pathParameters: {'songId': 123}); context.go(location); }, child: const Text('Go to song 2'), ),
To learn more about navigation, see the Navigation topic.
To redirect to a named route, use the namedLocation
API:
redirect: (BuildContext context, GoRouterState state) { if (AuthState.of(context).isSignedIn) { return context.namedLocation('signIn'); } else { return null; } },
To learn more about redirection, see the Redirection topic.