cleanRouteName method

String cleanRouteName(
  1. String name
)

Cleans up and formats the route name by removing unwanted characters and ensuring it is properly formatted as a valid URI.

name The route name to be cleaned and formatted.

Returns a valid URI string, or the original name if parsing fails.

Implementation

String cleanRouteName(String name) {
  // Edge case: Handle empty or null route names.
  if (name.isEmpty) {
    return '/'; // Default route or handle as needed.
  }

  // Remove unwanted parts of the route name.
  name = name.replaceAll('() => ', '');

  // Ensure the route starts with a '/'.
  if (!name.startsWith('/')) {
    name = '/$name';
  }

  // Try parsing the route name into a valid URI and return it.
  Uri? uri = Uri.tryParse(name);
  return uri?.toString() ?? name; // Return the original name if invalid URI.
}