new Uri.directory #17065
Labels
area-core-library
SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.
library-core
type-enhancement
A request for a change that isn't a bug
Converting a native directory path to a URI is error prone. The method Uri.resolve will strip the last path segment, and if one is aware of this and takes care to always append slashes to URIs representing a directory, Uri.resolve is rather powerful:
Convert relative file name [path] to absolute file name:
Uri.base.resolve(new Uri.file(path))
Get the directory name of file name [path]:
new Uri.file(path).resolve('.')
Get the parent directory of file name [path]:
new Uri.file(path).resolve('..')
However, if one has a native path to a directory, the above doesn't work if [path] doesn't end with the native path separator. So one must take care to append a slash. The simplest way I have found, without importing Platform from dart:io is:
Uri nativeDirectoryToUri(String nativePath) {
Uri uri = new Uri.file(nativePath);
String path = uri.path;
return (path == '' || path.endsWith('/'))
? uri
: Uri.parse('$uri/');
}
I propose this gets added to the Uri class, for example, as a factory analogous to Uri.file.
The text was updated successfully, but these errors were encountered: