goto static method

dynamic goto(
  1. String routeName, {
  2. dynamic parameter,
})

Implementation

static dynamic goto(String routeName, {dynamic parameter}) {
  assert(routeName.isNotEmpty);

  final activationRouteContext = ActivationRouteContext(routeName, parameter);

  /// Looking for route activation with routeName exact matching
  var canActivatedEntry = Route._routeActivationMap.entries.firstWhere(
      (entry) => entry.value.contains(routeName),
      orElse: () => MapEntry(Route.defaultRouteActivation, []));
  if (!Function.apply(canActivatedEntry.key, [activationRouteContext])) {
    /// Route Guard refused access to this routes.
    return;
  }

  KareeRouter._currentRoute = routeName;

  /// We reset the path variable for this call Session.
  _pathVariables = null;

  _RouteEntry? routeEntry = findActionFor(routeName);

  /// After checking action, we got nullable _RouteEntry, if present,
  /// then we are sure that the _route entry is exact match of current
  /// routeName or generic for (route with parameter) of routeName,
  /// then we apply routeGuard
  if (!(Function.apply(
      (routeEntry?.activation ?? Route.defaultRouteActivation),
      [activationRouteContext]))) {
    /// Route Guard refused access to this routes
    return;
  }
  try {
    if (routeEntry != null) {
      if (routeEntry.action is Function) {
        if (parameter == null) {
          return routeEntry.action();
        } else {
          return Function.apply(
              routeEntry.action,
              parameter is List
                  ? parameter
                  : parameter == null
                      ? []
                      : [parameter]);
        }
        // action(parameter);
      }

      throw NoActionFoundError(routeName, parameter);
    } else {
      ///
      /// When the route is not found! we will try to check whether it starts
      /// with the path that represents a module not loaded yet.
      ///
      try {
        var subscription = KareeModule.modules.entries.firstWhere(
            (subscription) => routeName.startsWith(subscription.key));
        if (subscription.value.isInitialized == false) {
          return Future.sync(() async {
            await subscription.value.initialize();
            return await goto(routeName, parameter: parameter);
          });
        }
        // ignore: empty_catches
      } on StateError {}
      throw NoRouteFoundError(routeName, parameter);
      //('No action for this route');
    }
  } catch (e, st) {
    if (e is NoActionFoundError || e is NoRouteFoundError) {
      screen(KareeConstants.kareeErrorScreenName, RouteMode.PUSH, argument: {
        #title: (e as dynamic).message,
        #stack: st,
        #env: [routeName, if (parameter != null) parameter],
        #errorCode: KareeErrorCode.noRouteFound
      });
    } else {
      screen(KareeConstants.kareeErrorScreenName, RouteMode.PUSH, argument: {
        #title: (e as dynamic).message,
        #stack: st,
        #env: [routeName, if (parameter != null) parameter],
        #errorCode: KareeErrorCode.generalError
      });
    }
  }
}