You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
I strongly agree with adding a constructor Uri.directory, similar to Uri.file, except that it guarantees that the path of the Uri ends in /, unless the path is the empty string.
This is the largest pain point we have found in using file: URIs to handle paths everywhere.
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: