cleanRouteName function

String cleanRouteName(
  1. String name
)

Takes a route name String generated by to, off, offAll (and similar context navigation methods), cleans the extra chars and accommodates the format.

This function cleans and formats the route name string to adhere to a more appealing URL naming convention, such as kebab case.

Example:

void main() {
  String routeName = "() => MyHomeScreenView";
  String cleanedRoute = cleanRouteName(routeName);
  print(cleanedRoute); // Output: '/my-home-screen-view'
}

In the above example, the cleanRouteName function takes a route name string generated by context navigation methods, removes any extra characters, converts it to kebab case, and prepends a forward slash if necessary. The cleaned route name is then returned.

Implementation

String cleanRouteName(String name) {
  name = name.replaceAll('() => ', '');

  // Convert the route name to kebab case
  // name = name.paramCase!;

  if (!name.startsWith('/')) {
    name = '/$name';
  }

  // Optionally, parse the route name as a URI and convert it to a string
  return Uri.tryParse(name)?.toString() ?? name;
}