parseRouteInformation method

  1. @override
Future<AppRoutePath> 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<AppRoutePath> parseRouteInformation(
    RouteInformation routeInformation) async {
  final path = routeInformation.location;

  //
  if (path == null) {
    return AppRoutePath.home();
  }

  // It's the very same path??
  if (path == AppRouterDelegate._currentConfiguration.path) {
    return AppRouterDelegate._currentConfiguration;
  }

  final uri = Uri.parse(path);

  if (uri.pathSegments.isEmpty) {
    return AppRoutePath.home();
  }

  // Handle '/book/:id'
  if (uri.pathSegments.length == 2) {
    //
    // if (!uri.pathSegments[0].startsWith('/')) {
    //   return AppRoutePath.unknown();
    // }

    var remaining = uri.pathSegments[1];

    if (remaining.isEmpty) {
      return AppRoutePath.unknown();
    }

    if (!remaining.startsWith('/')) {
      remaining = '/$remaining';
    }

    return AppRoutePath.page(remaining);
  } else if (uri.pathSegments.length == 1) {
    //
    return AppRoutePath.page(uri.path);
  }
  // Handle unknown routes
  return AppRoutePath.unknown(path);
}