GoRouter class Get started Upgrading Configuration Navigation Redirection Web Deep linking Named routes Error handling

The route configuration for the app.

The routes list specifies the top-level routes for the app. It must not be empty and must contain an GoRoute to match /.

See the Get started example, which shows an app with a simple route configuration.

The redirect callback allows the app to redirect to a new location. Alternatively, you can specify a redirect for an individual route using GoRoute.redirect. If BuildContext.dependOnInheritedWidgetOfExactType is used during the redirection (which is how of methods are usually implemented), a re-evaluation will be triggered when the InheritedWidget changes.

To handle exceptions, use one of onException, errorBuilder, or errorPageBuilder. The onException is called when an exception is thrown. If onException is not provided, the exception is passed to errorPageBuilder to build a page for the Router if it is not null; otherwise, it is passed to errorBuilder instead. If none of them are provided, go_router builds a default error screen to show the exception. See Error handling for more details.

To disable automatically requesting focus when new routes are pushed to the navigator, set requestFocus to false.

See also:

  • Configuration
  • GoRoute, which provides APIs to define the routing table.
  • examples, which contains examples for different routing scenarios.
Implemented types

Constructors

GoRouter({required List<RouteBase> routes, Codec<Object?, Object?>? extraCodec, GoExceptionHandler? onException, GoRouterPageBuilder? errorPageBuilder, GoRouterWidgetBuilder? errorBuilder, GoRouterRedirect? redirect, Listenable? refreshListenable, int redirectLimit = 5, bool routerNeglect = false, String? initialLocation, bool overridePlatformDefaultLocation = false, Object? initialExtra, List<NavigatorObserver>? observers, bool debugLogDiagnostics = false, GlobalKey<NavigatorState>? navigatorKey, String? restorationScopeId, bool requestFocus = true})
Default constructor to configure a GoRouter with a routes builder and an error page builder.
factory
GoRouter.routingConfig({required ValueListenable<RoutingConfig> routingConfig, Codec<Object?, Object?>? extraCodec, GoExceptionHandler? onException, GoRouterPageBuilder? errorPageBuilder, GoRouterWidgetBuilder? errorBuilder, Listenable? refreshListenable, bool routerNeglect = false, String? initialLocation, bool overridePlatformDefaultLocation = false, Object? initialExtra, List<NavigatorObserver>? observers, bool debugLogDiagnostics = false, GlobalKey<NavigatorState>? navigatorKey, String? restorationScopeId, bool requestFocus = true})
Creates a GoRouter with a dynamic RoutingConfig.

Properties

backButtonDispatcher BackButtonDispatcher
The BackButtonDispatcher that is used to configure the Router.
final
configuration RouteConfiguration
The route configuration used in go_router.
latefinal
hashCode int
The hash code for this object.
no setterinherited
overridePlatformDefaultLocation bool
Whether to ignore platform's default initial location when initialLocation is set.
final
routeInformationParser GoRouteInformationParser
The route information parser used by GoRouter.
latefinaloverride-getter
routeInformationProvider GoRouteInformationProvider
The route information provider used by GoRouter.
latefinaloverride-getter
routerDelegate GoRouterDelegate
The router delegate. Provide this to the MaterialApp or CupertinoApp's .router() constructor
latefinaloverride-getter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
state GoRouterState?
The top GoRouterState, the state of the route that was last used in either GoRouter.go or GoRouter.push.
no setter

Methods

canPop() bool
Returns true if there is at least two or more route can be pop.
dispose() → void
Disposes resource created by this object.
go(String location, {Object? extra}) → void
Navigate to a URI location w/ optional query parameters, e.g. /family/f2/person/p1?color=blue
goNamed(String name, {Map<String, String> pathParameters = const <String, String>{}, Map<String, dynamic> queryParameters = const <String, dynamic>{}, Object? extra}) → void
Navigate to a named route w/ optional parameters, e.g. name='person', pathParameters={'fid': 'f2', 'pid': 'p1'} Navigate to the named route.
namedLocation(String name, {Map<String, String> pathParameters = const <String, String>{}, Map<String, dynamic> queryParameters = const <String, dynamic>{}}) String
Get a location from route name and parameters. This is useful for redirecting to a named location.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
pop<T extends Object?>([T? result]) → void
Pop the top-most route off the current screen.
push<T extends Object?>(String location, {Object? extra}) Future<T?>
Push a URI location onto the page stack w/ optional query parameters, e.g. /family/f2/person/p1?color=blue.
pushNamed<T extends Object?>(String name, {Map<String, String> pathParameters = const <String, String>{}, Map<String, dynamic> queryParameters = const <String, dynamic>{}, Object? extra}) Future<T?>
Push a named route onto the page stack w/ optional parameters, e.g. name='person', pathParameters={'fid': 'f2', 'pid': 'p1'}
pushReplacement<T extends Object?>(String location, {Object? extra}) Future<T?>
Replaces the top-most page of the page stack with the given URL location w/ optional query parameters, e.g. /family/f2/person/p1?color=blue.
pushReplacementNamed<T extends Object?>(String name, {Map<String, String> pathParameters = const <String, String>{}, Map<String, dynamic> queryParameters = const <String, dynamic>{}, Object? extra}) Future<T?>
Replaces the top-most page of the page stack with the named route w/ optional parameters, e.g. name='person', pathParameters={'fid': 'f2', 'pid': 'p1'}.
refresh() → void
Refresh the route.
replace<T>(String location, {Object? extra}) Future<T?>
Replaces the top-most page of the page stack with the given one but treats it as the same page.
replaceNamed<T>(String name, {Map<String, String> pathParameters = const <String, String>{}, Map<String, dynamic> queryParameters = const <String, dynamic>{}, Object? extra}) Future<T?>
Replaces the top-most page with the named route and optional parameters, preserving the page key.
restore(RouteMatchList matchList) → void
Restore the RouteMatchList
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Properties

optionURLReflectsImperativeAPIs bool
Whether the imperative API affects browser URL bar.
getter/setter pair

Static Methods

maybeOf(BuildContext context) GoRouter?
The current GoRouter in the widget tree, if any.
of(BuildContext context) GoRouter
Find the current GoRouter in the widget tree.