validatePath method
Normalizes a file system path so it can be safely consumed by dart:io.
- Returns
nullwhenpathisnullor empty. - Prepends a leading
/on non-Windows platforms when missing. - Removes a single trailing separator (
/or\\). - Collapses repeated separators into a single platform-specific one.
Implementation
String? validatePath(String? path) {
if (path == null || path.isEmpty) return null;
String normalized = path;
if (!Platform.isWindows && !normalized.startsWith('/')) {
normalized = '/$normalized';
}
if (normalized.endsWith('/') || normalized.endsWith(r'\')) {
normalized = normalized.substring(0, normalized.length - 1);
}
return normalized.replaceAll(RegExp(r'[/\\]+'), pathJoin);
}