GoRouter constructor

GoRouter({
  1. required List<RouteBase> routes,
  2. GoExceptionHandler? onException,
  3. GoRouterPageBuilder? errorPageBuilder,
  4. GoRouterWidgetBuilder? errorBuilder,
  5. GoRouterRedirect? redirect,
  6. Listenable? refreshListenable,
  7. int redirectLimit = 5,
  8. bool routerNeglect = false,
  9. String? initialLocation,
  10. Object? initialExtra,
  11. List<NavigatorObserver>? observers,
  12. bool debugLogDiagnostics = false,
  13. GlobalKey<NavigatorState>? navigatorKey,
  14. String? restorationScopeId,
  15. 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;
  }());
}