GoRouter class

The top-level go router class.

This is the main entry point for defining app's routing policy.

The routes defines the routing table. It must not be empty and must contain an GoRouter to match /.

See Routes for an example of defining a simple routing table.

See Sub-routes for an example of defining a multi-level routing table.

The redirect does top-level redirection before the URIs are parsed by the routes. Consider using GoRoute.redirect for individual route redirection.

See also:

  • GoRoute, which provides APIs to define the routing table.
  • examples, which contains examples for different routing scenarios.
Inheritance
Mixed-in types

Constructors

GoRouter({required List<RouteBase> routes, GoRouterPageBuilder? errorPageBuilder, GoRouterWidgetBuilder? errorBuilder, GoRouterRedirect? redirect, Listenable? refreshListenable, int redirectLimit = 5, bool routerNeglect = false, String? initialLocation, UrlPathStrategy? urlPathStrategy, List<NavigatorObserver>? observers, bool debugLogDiagnostics = false, GoRouterNavigatorBuilder? navigatorBuilder, GlobalKey<NavigatorState>? navigatorKey, String? restorationScopeId})
Default constructor to configure a GoRouter with a routes builder and an error page builder.

Properties

hashCode int
The hash code for this object.
no setterinherited
hasListeners bool
Whether any listeners are currently registered.
no setterinherited
location String
Get the current location.
no setter
The navigator that the observer is observing, if any.
no setterinherited
routeConfiguration → RouteConfiguration
The route configuration. Used for testing.
no setter
routeInformationParser → GoRouteInformationParser
The route information parser used by GoRouter.
no setter
routeInformationProvider → GoRouteInformationProvider
The route information provider used by GoRouter.
no setter
routerDelegate → GoRouterDelegate
The router delegate. Provide this to the MaterialApp or CupertinoApp's .router() constructor
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

addListener(VoidCallback listener) → void
Register a closure to be called when the object changes.
inherited
canPop() bool
Returns true if there is more than 1 page on the stack.
didPop(Route route, Route? previousRoute) → void
The Navigator popped route.
override
didPush(Route route, Route? previousRoute) → void
The Navigator pushed route.
override
didRemove(Route route, Route? previousRoute) → void
The Navigator removed route.
override
didReplace({Route? newRoute, Route? oldRoute}) → void
The Navigator replaced oldRoute with newRoute.
override
didStartUserGesture(Route route, Route? previousRoute) → void
The Navigator's routes are being moved by a user gesture.
inherited
didStopUserGesture() → void
User gesture is no longer controlling the Navigator.
inherited
dispose() → void
Discards any resources used by the object. After this is called, the object is not in a usable state and should be discarded (calls to addListener will throw after the object is disposed).
override
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> params = const <String, String>{}, Map<String, dynamic> queryParams = const <String, dynamic>{}, Object? extra}) → void
Navigate to a named route w/ optional parameters, e.g. name='person', params={'fid': 'f2', 'pid': 'p1'} Navigate to the named route.
namedLocation(String name, {Map<String, String> params = const <String, String>{}, Map<String, dynamic> queryParams = 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
notifyListeners() → void
Call all the registered listeners.
inherited
pop() → void
Pop the top page off the GoRouter's page stack.
push(String location, {Object? extra}) → void
Push a URI location onto the page stack w/ optional query parameters, e.g. /family/f2/person/p1?color=blue
pushNamed(String name, {Map<String, String> params = const <String, String>{}, Map<String, dynamic> queryParams = const <String, dynamic>{}, Object? extra}) → void
Push a named route onto the page stack w/ optional parameters, e.g. name='person', params={'fid': 'f2', 'pid': 'p1'}
refresh() → void
Refresh the route.
removeListener(VoidCallback listener) → void
Remove a previously registered closure from the list of closures that are notified when the object changes.
inherited
replace(String location, {Object? extra}) → void
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.
replaceNamed(String name, {Map<String, String> params = const <String, String>{}, Map<String, dynamic> queryParams = const <String, dynamic>{}, Object? extra}) → void
Replaces the top-most page of the page stack with the named route w/ optional parameters, e.g. name='person', params={'fid': 'f2', 'pid': 'p1'}.
toString() String
A string representation of this object.
inherited

Operators

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

Static Methods

of(BuildContext context) GoRouter
Find the current GoRouter in the widget tree.
setUrlPathStrategy(UrlPathStrategy strategy) → void
Set the app's URL path strategy (defaults to hash). call before runApp().