parseRouteInformation method

  1. @override
Future<AppRouteState> parseRouteInformation(
  1. RouteInformation routeInformation
)
override

Converts the given route information into parsed data to pass to a RouterDelegate.

The method should return a future which completes when the parsing is complete. The parsing may be asynchronous if, e.g., the parser needs to communicate with the OEM thread to obtain additional data about the route.

Consider using a SynchronousFuture if the result can be computed synchronously, so that the Router does not need to wait for the next microtask to pass the data to the RouterDelegate.

One can implement parseRouteInformationWithDependencies instead if the parsing depends on other dependencies from the BuildContext.

Implementation

@override
Future<AppRouteState> parseRouteInformation(
  RouteInformation routeInformation,
) {
  final currentRoute = _decodeRoute(routeInformation.uri.toString());
  final appRoutes = <AppRootRoute, AppRoute>{};
  var appRootRoute = AppRootRoute.fleet;
  var fleetView = FleetView.map;
  var fleetShowingDashboard = false;

  final state = routeInformation.state;
  if (state is Map) {
    final stateAppRoutes = state['appRoutes'];
    if (stateAppRoutes is Map) {
      for (final entry in stateAppRoutes.entries) {
        final key = entry.key;
        final value = entry.value;

        if (key is String && value is String) {
          final screenRoute = _decodeRootRoute(key);
          final appRoute = _decodeRoute(value);
          assert(screenRoute.appRoute == appRoute.root);

          appRoutes[screenRoute] = appRoute;
        }
      }
    }

    final stateRootRoute = state['appRootRoute'];
    if (stateRootRoute is String) {
      appRootRoute = _decodeRootRoute(stateRootRoute);
    }

    final stateFleetView = state['fleetView'];
    if (stateFleetView is String) {
      fleetView = FleetView.fromValue(stateFleetView) ?? fleetView;
    }

    final stateFleetShowingDashboard = state['fleetShowingDashboard'];
    if (stateFleetShowingDashboard is bool) {
      fleetShowingDashboard = stateFleetShowingDashboard;
    }
  }

  // Query parameters have priority for fleet route state
  final params = routeInformation.uri.queryParameters;
  fleetView = FleetView.fromValue(params['view']) ?? fleetView;
  fleetShowingDashboard =
      _tryParseBool(params['showingDashboard']) ?? fleetShowingDashboard;

  return SynchronousFuture(
    AppRouteState(
      appRoutes: appRoutes,
      appRootRoute: appRootRoute,
      current: AppRouteState.isRootRoute(currentRoute) ? null : currentRoute,
      fleetView: fleetView,
      fleetShowingDashboard: fleetShowingDashboard,
    ),
  );
}