match static method

RouteMatch? match({
  1. required RouteBase route,
  2. required String remainingLocation,
  3. required String matchedLocation,
  4. required Map<String, String> pathParameters,
})

Generate a RouteMatch object by matching the route with remainingLocation.

The extracted path parameters, as the result of the matching, are stored into pathParameters.

Implementation

static RouteMatch? match({
  required RouteBase route,
  required String remainingLocation, // e.g. person/p1
  required String matchedLocation, // e.g. /family/f2
  required Map<String, String> pathParameters,
}) {
  if (route is ShellRouteBase) {
    return RouteMatch(
      route: route,
      matchedLocation: remainingLocation,
      pageKey: ValueKey<String>(route.hashCode.toString()),
    );
  } else if (route is GoRoute) {
    assert(!route.path.contains('//'));

    final RegExpMatch? match = route.matchPatternAsPrefix(remainingLocation);
    if (match == null) {
      return null;
    }

    final Map<String, String> encodedParams = route.extractPathParams(match);
    for (final MapEntry<String, String> param in encodedParams.entries) {
      pathParameters[param.key] = Uri.decodeComponent(param.value);
    }
    final String pathLoc = patternToPath(route.path, encodedParams);
    final String newMatchedLocation =
        concatenatePaths(matchedLocation, pathLoc);
    return RouteMatch(
      route: route,
      matchedLocation: newMatchedLocation,
      pageKey: ValueKey<String>(route.hashCode.toString()),
    );
  }
  assert(false, 'Unexpected route type: $route');
  return null;
}