match method

DeepLinkMatch? match(
  1. Uri appUri,
  2. Iterable<RouteInfo> routes, {
  3. Uri? original,
})

Match a normalised URI against all routes.

Returns null if no route accepts the deep link.

Implementation

DeepLinkMatch? match(
  Uri appUri,
  Iterable<RouteInfo> routes, {
  Uri? original,
}) {
  final effectiveRoutes = routes
      .where((route) => route.deepLinkAllowed)
      .toList(growable: false);

  final segments = appUri.pathSegments;

  if (segments.isNotEmpty) {
    for (final route in effectiveRoutes) {
      final templateSegments = _templateSegments(route.path);
      if (_matchesTemplate(segments, templateSegments)) {
        return DeepLinkMatch(
          route: route,
          args: _buildArgs(route, appUri, original),
        );
      }
    }
  }

  if (segments.isNotEmpty) {
    final key = segments.first.toLowerCase();
    for (final route in effectiveRoutes) {
      final aliases = route.deepLinkNames.map((alias) => alias.toLowerCase());
      if (aliases.contains(key)) {
        return DeepLinkMatch(
          route: route,
          args: _buildArgs(route, appUri, original),
        );
      }
    }
  }

  return null;
}