GoRouter constructor
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,
- String? restorationScopeId,
Default constructor to configure a GoRouter with a routes builder and an error page builder.
The routes
must not be null and must contain an GoRouter to match /
.
Implementation
GoRouter({
required List<RouteBase> routes,
// TODO(johnpryan): Change to a route, improve error API
// See https://github.com/flutter/flutter/issues/108144
GoRouterPageBuilder? errorPageBuilder,
GoRouterWidgetBuilder? errorBuilder,
GoRouterRedirect? redirect,
Listenable? refreshListenable,
int redirectLimit = 5,
bool routerNeglect = false,
String? initialLocation,
// TODO(johnpryan): Deprecate this parameter
// See https://github.com/flutter/flutter/issues/108132
UrlPathStrategy? urlPathStrategy,
List<NavigatorObserver>? observers,
bool debugLogDiagnostics = false,
// TODO(johnpryan): Deprecate this parameter
// See https://github.com/flutter/flutter/issues/108145
GoRouterNavigatorBuilder? navigatorBuilder,
GlobalKey<NavigatorState>? navigatorKey,
String? restorationScopeId,
}) {
if (urlPathStrategy != null) {
setUrlPathStrategy(urlPathStrategy);
}
setLogging(enabled: debugLogDiagnostics);
WidgetsFlutterBinding.ensureInitialized();
navigatorKey ??= GlobalKey<NavigatorState>();
_routeConfiguration = RouteConfiguration(
routes: routes,
topRedirect: redirect ?? (_) => null,
redirectLimit: redirectLimit,
navigatorKey: navigatorKey,
);
_routeInformationParser = GoRouteInformationParser(
configuration: _routeConfiguration,
debugRequireGoRouteInformationProvider: true,
);
_routeInformationProvider = GoRouteInformationProvider(
initialRouteInformation: RouteInformation(
location: _effectiveInitialLocation(initialLocation)),
refreshListenable: refreshListenable);
_routerDelegate = GoRouterDelegate(
configuration: _routeConfiguration,
errorPageBuilder: errorPageBuilder,
errorBuilder: errorBuilder,
routerNeglect: routerNeglect,
observers: <NavigatorObserver>[
...observers ?? <NavigatorObserver>[],
this
],
restorationScopeId: restorationScopeId,
// wrap the returned Navigator to enable GoRouter.of(context).go() et al,
// allowing the caller to wrap the navigator themselves
builderWithNav:
(BuildContext context, GoRouterState state, Navigator nav) =>
InheritedGoRouter(
goRouter: this,
child: navigatorBuilder?.call(context, state, nav) ?? nav,
),
);
assert(() {
log.info('setting initial location $initialLocation');
return true;
}());
}