GoRouter constructor
GoRouter({
- required List<
RouteBase> routes, - GoExceptionHandler? onException,
- GoRouterPageBuilder? errorPageBuilder,
- GoRouterWidgetBuilder? errorBuilder,
- GoRouterRedirect? redirect,
- Listenable? refreshListenable,
- int redirectLimit = 5,
- bool routerNeglect = false,
- String? initialLocation,
- Object? initialExtra,
- List<
NavigatorObserver> ? observers, - bool debugLogDiagnostics = false,
- String? restorationScopeId,
- bool requestFocus = true,
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,
GoExceptionHandler? onException,
GoRouterPageBuilder? errorPageBuilder,
GoRouterWidgetBuilder? errorBuilder,
GoRouterRedirect? redirect,
Listenable? refreshListenable,
int redirectLimit = 5,
bool routerNeglect = false,
String? initialLocation,
Object? initialExtra,
List<NavigatorObserver>? observers,
bool debugLogDiagnostics = false,
GlobalKey<NavigatorState>? navigatorKey,
String? restorationScopeId,
bool requestFocus = true,
}) : backButtonDispatcher = RootBackButtonDispatcher(),
assert(
initialExtra == null || initialLocation != null,
'initialLocation must be set in order to use initialExtra',
),
assert(
(onException == null ? 0 : 1) +
(errorPageBuilder == null ? 0 : 1) +
(errorBuilder == null ? 0 : 1) <
2,
'Only one of onException, errorPageBuilder, or errorBuilder can be provided.'),
assert(_debugCheckPath(routes, true)),
assert(
_debugVerifyNoDuplicatePathParameter(routes, <String, GoRoute>{})),
assert(_debugCheckParentNavigatorKeys(
routes,
navigatorKey == null
? <GlobalKey<NavigatorState>>[]
: <GlobalKey<NavigatorState>>[navigatorKey])) {
setLogging(enabled: debugLogDiagnostics);
WidgetsFlutterBinding.ensureInitialized();
navigatorKey ??= GlobalKey<NavigatorState>();
configuration = RouteConfiguration(
routes: routes,
topRedirect: redirect ?? (_, __) => null,
redirectLimit: redirectLimit,
navigatorKey: navigatorKey,
);
final ParserExceptionHandler? parserExceptionHandler;
if (onException != null) {
parserExceptionHandler =
(BuildContext context, RouteMatchList routeMatchList) {
onException(context,
configuration.buildTopLevelGoRouterState(routeMatchList), this);
// Avoid updating GoRouterDelegate if onException is provided.
return routerDelegate.currentConfiguration;
};
} else {
parserExceptionHandler = null;
}
routeInformationParser = GoRouteInformationParser(
onParserException: parserExceptionHandler,
configuration: configuration,
);
routeInformationProvider = GoRouteInformationProvider(
initialLocation: _effectiveInitialLocation(initialLocation),
initialExtra: initialExtra,
refreshListenable: refreshListenable,
);
routerDelegate = GoRouterDelegate(
configuration: configuration,
errorPageBuilder: errorPageBuilder,
errorBuilder: errorBuilder,
routerNeglect: routerNeglect,
observers: <NavigatorObserver>[
...observers ?? <NavigatorObserver>[],
],
restorationScopeId: restorationScopeId,
requestFocus: requestFocus,
// wrap the returned Navigator to enable GoRouter.of(context).go() et al,
// allowing the caller to wrap the navigator themselves
builderWithNav: (BuildContext context, Widget child) =>
InheritedGoRouter(goRouter: this, child: child),
);
assert(() {
log.info('setting initial location $initialLocation');
return true;
}());
}