matchRoute method

AppRouteMatch<dynamic, RouteParams>? matchRoute(
  1. String? path
)

Implementation

AppRouteMatch? matchRoute(String? path) {
  if (path == null) return null;
  final uri = Uri.parse(path);
  final components = path == Navigator.defaultRouteName ? const ["/"] : uri.pathSegments;

  Map<RouteTreeNode, RouteTreeNodeMatch?> nodeMatches = <RouteTreeNode, RouteTreeNodeMatch>{};
  var nodesToCheck = _nodes;
  RouteTreeNodeMatch? match;
  for (final segment in components) {
    final currentMatches = <RouteTreeNode, RouteTreeNodeMatch?>{};
    final nextNodes = <RouteTreeNode>[];
    for (final node in nodesToCheck) {
      bool isMatch = (node.part == segment || node.isParameter());
      if (isMatch) {
        final parentMatch = nodeMatches[node.parent];
        match = RouteTreeNodeMatch.fromMatch(parentMatch, node);
        if (node.isParameter()) {
          String? paramKey = parameterType.extractName(node.part);
          match[paramKey] = segment;
        }

//          print("matched: ${node.part}, isParam: ${node.isParameter()}, params: ${match.parameters}");
        currentMatches[node] = match;
        nextNodes.addAll(node.nodes);
      }
    }

    nodeMatches = currentMatches;
    nodesToCheck = nextNodes;
    if (currentMatches.values.isEmpty) {
      return null;
    }
  }
  if (match != null) {
    match += uri.queryParametersAll;
  }
  List<RouteTreeNodeMatch> matches = nodeMatches.values.notNullList();
  for (final match in matches) {
    RouteTreeNode nodeToUse = match.node;
    _log.fine("using match: $match, ${nodeToUse.part}, ${match._parameters}");
    if (nodeToUse.routes.isNotEmpty) {
      final routes = nodeToUse.routes;
      final routeMatch = AppRouteMatch.ofMap(routes[0], sanitizeParams(match._parameters));
      return routeMatch;
    }
  }
  return null;
}