safePathSegments property

List<String> get safePathSegments

Like Uri.pathSegments, but never throws a FormatException when the path contains malformed percent-encoded sequences (e.g. /%AF/zsssss).

Segments that cannot be decoded as valid UTF-8 are decoded leniently: malformed bytes are replaced with the Unicode replacement character (�) instead of throwing.

Implementation

List<String> get safePathSegments {
  try {
    return pathSegments;
  } on FormatException {
    var raw = path;
    if (raw.startsWith('/')) raw = raw.substring(1);
    if (raw.isEmpty) return const [];
    return List.unmodifiable(raw.split('/').map(safeDecodeUriComponent));
  }
}