GoRouter.routingConfig constructor

GoRouter.routingConfig({
  1. required ValueListenable<RoutingConfig> routingConfig,
  2. Codec<Object?, Object?>? extraCodec,
  3. GoExceptionHandler? onException,
  4. GoRouterPageBuilder? errorPageBuilder,
  5. GoRouterWidgetBuilder? errorBuilder,
  6. Listenable? refreshListenable,
  7. bool routerNeglect = false,
  8. String? initialLocation,
  9. bool overridePlatformDefaultLocation = false,
  10. Object? initialExtra,
  11. List<NavigatorObserver>? observers,
  12. bool debugLogDiagnostics = false,
  13. GlobalKey<NavigatorState>? navigatorKey,
  14. String? restorationScopeId,
  15. bool requestFocus = true,
})

Creates a GoRouter with a dynamic RoutingConfig.

See routing_config.dart.

Implementation

GoRouter.routingConfig({
  required ValueListenable<RoutingConfig> routingConfig,
  Codec<Object?, Object?>? extraCodec,
  GoExceptionHandler? onException,
  GoRouterPageBuilder? errorPageBuilder,
  GoRouterWidgetBuilder? errorBuilder,
  Listenable? refreshListenable,
  bool routerNeglect = false,
  String? initialLocation,
  this.overridePlatformDefaultLocation = false,
  Object? initialExtra,
  List<NavigatorObserver>? observers,
  bool debugLogDiagnostics = false,
  GlobalKey<NavigatorState>? navigatorKey,
  String? restorationScopeId,
  bool requestFocus = true,
})  : _routingConfig = routingConfig,
      backButtonDispatcher = RootBackButtonDispatcher(),
      assert(
        initialExtra == null || initialLocation != null,
        'initialLocation must be set in order to use initialExtra',
      ),
      assert(!overridePlatformDefaultLocation || initialLocation != null,
          'Initial location must be set to override platform default'),
      assert(
          (onException == null ? 0 : 1) +
                  (errorPageBuilder == null ? 0 : 1) +
                  (errorBuilder == null ? 0 : 1) <
              2,
          'Only one of onException, errorPageBuilder, or errorBuilder can be provided.') {
  setLogging(enabled: debugLogDiagnostics);
  WidgetsFlutterBinding.ensureInitialized();

  navigatorKey ??= GlobalKey<NavigatorState>();

  _routingConfig.addListener(_handleRoutingConfigChanged);
  configuration = RouteConfiguration(
    _routingConfig,
    navigatorKey: navigatorKey,
    extraCodec: extraCodec,
  );

  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('setting initial location $initialLocation');
    return true;
  }());
}