normalizeTrailingSlash method

Uri normalizeTrailingSlash({
  1. bool trailingSlash = true,
})

Normalizes the trailing slash in the path.

If trailingSlash is true, ensures the path ends with a slash. If false, removes any trailing slashes.

Implementation

Uri normalizeTrailingSlash({bool trailingSlash = true}) {
  final currentPath = path;
  if (currentPath.isEmpty) return this;

  if (trailingSlash) {
    if (currentPath.endsWith('/')) return this;
    return replace(path: '$currentPath/');
  }

  final normalized = currentPath.replaceAll(RegExp(r'/+$'), '');
  return normalized == currentPath ? this : replace(path: normalized);
}