HyperRouter constructor

HyperRouter({
  1. required RouteValue initialRoute,
  2. required List<HyperRoute<RouteValue>> routes,
  3. bool enableUrl = false,
  4. RouteValue? onException(
    1. OnExceptionState state
    )?,
  5. RouteValue? redirect(
    1. BuildContext context,
    2. RedirectState state
    ) = _defaultRedirect,
})

Implementation

HyperRouter({
  required RouteValue initialRoute,
  required this.routes,

  /// If enabled, the router will try to create URLs for routes it navigates
  /// to, or parse URLs it receives from the browser. Make sure to provide
  /// url parsers for routes that need them.
  bool enableUrl = false,

  /// Triggered when an exeption is thrown during URL deserialization.
  ///
  /// Return the value for the route you wish to redirect to (e.g. to display
  /// an error message), or null to let the exception propagate further.
  RouteValue? Function(OnExceptionState state)? onException,
  this.redirect = _defaultRedirect,
}) : onException = onException ?? _defaultOnException {
  for (final r in routes) {
    r.parent = null;
  }

  final routeMap = <Object, HyperRoute>{};
  for (final r in routes) {
    r.forEach((r) {
      if (routeMap.containsKey(r.key)) {
        throw HyperError('Duplicate key detected: ${r.key}');
      }

      routeMap[r.key] = r;
    });
  }

  rootController = RootHyperController(
    initialRoute: initialRoute,
    redirect: redirect,
    routeMap: routeMap,
  );

  routerDelegate = HyperRouterDelegate(
    initialRoute: initialRoute,
    routerConfig: this,
  );

  if (enableUrl) {
    routeInformationParser = HyperRouteInformationParser(
      config: this,
    );

    final u = routeInformationParser!
        .restoreRouteInformation(rootController.stack)!;

    routeInformationProvider = PlatformRouteInformationProvider(
      initialRouteInformation: u,
    );
  } else {
    routeInformationParser = null;
    routeInformationProvider = null;
  }
}