getDirname method

String getDirname({
  1. Context? pathContext,
})

Gets the part of path before the last separator.

File('path/to/foo.dart').getDirname(); // -> 'path/to'
Directory('path/to').getDirname();     // -> 'path'

Trailing separators are ignored.

Directory('path/to/').getDirname(); // -> 'path'

If an absolute path contains no directories, only a root, then the root is returned.

Directory('/').getDirname();   // -> '/' (posix)
Directory(r'c:\').getDirname(); // -> 'c:\' (windows)

If a relative path has no directories, then '.' is returned.

Directory('foo').getDirname();  // -> '.'
Directory('').getDirname();  // -> '.'

Implementation

String getDirname({
  lib_path.Context? pathContext,
}) {
  pathContext ??= lib_path.context;
  return pathContext.dirname(path);
}