HyperRouter constructor
HyperRouter({
- required RouteValue initialRoute,
- required List<
HyperRoute< routes,RouteValue> > - bool enableUrl = false,
- RouteValue? onException(
- OnExceptionState state
- RouteValue? redirect(
- BuildContext context,
- RedirectState state
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;
}
}