toSlug method

String toSlug()

Converts a string into a URL-friendly slug

Example:

"Hello World!".toSlug(); // Returns "hello-world"
"Product Name (2023)".toSlug(); // Returns "product-name-2023"

Implementation

String toSlug() => toLowerCase()
    .trim()
    .replaceAll(RegExp(r'[^\w\s-]'), '')
    .replaceAll(RegExp(r'\s+'), '-');