launchAction method

void launchAction(
  1. String action, {
  2. List<RouteParams>? args,
})

Launches a navigation or URL action based on the given action string. Supports various navigation types via the navigateType query parameter.

Support navigateType:

  • pop: /?navigateType=pop will be pop the current view
  • popFirstAndPush: profile?navigateType=popFirstAndPush pop to the first view and then push
  • pushReplacement: profile?navigateType=pushReplacement push and replacement
  • null || other: push

If the URI has a scheme (e.g., "https"), it opens the external URL. Otherwise, it attempts to navigate within the app using named routes.

Implementation

void launchAction(String action, {List<RouteParams>? args}) {
  // Try to parse the action as a URI. If invalid, exit early.
  final uri = Uri.tryParse(action);

  if (uri == null) {
    return;
  }

  // If the URI contains a scheme (e.g., "https", "mailto"), treat it as an external link.
  if (uri.scheme.isNotEmpty) {
    openUrl(uri);
    return;
  }

  final navigationType = uri.queryParameters['navigateType']?.toLowerCase();

  // Perform a pop operation
  if (navigationType == 'pop') {
    pop(args: (args ?? <RouteParams>[]).firstOrNull);
  }

  if (uri.path.isEmpty || !canNavigate(uri.path)) {
    return;
  }

  // Handle different navigation strategies based on `navigateType`.
  switch (navigationType) {
    case 'popfirstandpush':
      popToFirstAndPushNamed(uri.toString(), args: args ?? <RouteParams>[]);
      return;
    case 'pushreplacement':
      pushReplacementNamed(uri.toString(), args: args ?? <RouteParams>[]);
      return;
    default:
      pushNamed(uri.toString(), args: args ?? <RouteParams>[]);
      return;
  }
}