findActionFor static method

_RouteEntry? findActionFor(
  1. String ro
)

Function used to get the specific action from a path route.

This function also setup KareeRouter.pathVariables value when the path represented by this route contains url parameters.

Implementation

static _RouteEntry? findActionFor(String ro) {
  MapEntry<String, _RouteEntry?> routeEntry = Route._routeMap.entries
      .firstWhere((entry) => entry.key == ro,
          orElse: () => MapEntry('', null));
  if (routeEntry.value == null) {
    MapEntry<String, _ParameterizedRoute> arg = Route._routeWithParams.entries
        .firstWhere((entryParam) => RegExp(entryParam.key).hasMatch(ro),
            orElse: () => MapEntry(
                '',
                _ParameterizedRoute(
                    _RouteEntry('', null, (_) => false), [])));
    if (arg.value.action != null) {
      var pathVar = RegExp(arg.key)
          .allMatches(ro)
          .map((e) =>
              e.groups(List<int>.generate(e.groupCount + 1, (ind) => ind)))
          .firstWhere((element) => true, orElse: () => <String>[]);
      KareeRouter._pathVariables = {};
      for (int i = 1; i < pathVar.length; i++) {
        KareeRouter._pathVariables!.addEntries([
          MapEntry(
              Route._routeWithParams[arg.key]!.parameters[i - 1], pathVar[i]!)
        ]);
      }
      return arg.value.originalRoute;
    }
    return null;
  }
  return routeEntry.value;
}