normalizeRoutePath function

String normalizeRoutePath(
  1. String path
)

规范化路由 path,供守卫等基于路径的匹配使用。

  • 合并重复的 /
  • 去除除根路径外的尾部 /
  • 空路径归一为 /

调用方应传入 uri.path 而不是 uri.toString(),以忽略 query 和 fragment。

Normalizes a route path for guard matching.

Implementation

String normalizeRoutePath(String path) {
  if (path.isEmpty) {
    return '/';
  }

  var normalized = path.replaceAll(RegExp(r'/+'), '/');
  if (normalized.length > 1 && normalized.endsWith('/')) {
    normalized = normalized.substring(0, normalized.length - 1);
  }
  return normalized;
}