GoRouter constructor

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