current top-level property Null safety

String current

Gets the path to the current working directory.

In the browser, this means the current URL, without the last file segment.

Implementation

String get current {
  // If the current working directory gets deleted out from under the program,
  // accessing it will throw an IO exception. In order to avoid transient
  // errors, if we already have a cached working directory, catch the error and
  // use that.
  Uri uri;
  try {
    uri = Uri.base;
  } on Exception {
    if (_current != null) return _current!;
    rethrow;
  }

  // Converting the base URI to a file path is pretty slow, and the base URI
  // rarely changes in practice, so we cache the result here.
  if (uri == _currentUriBase) return _current!;
  _currentUriBase = uri;

  if (Style.platform == Style.url) {
    _current = uri.resolve('.').toString();
  } else {
    final path = uri.toFilePath();
    // Remove trailing '/' or '\' unless it is the only thing left
    // (for instance the root on Linux).
    final lastIndex = path.length - 1;
    assert(path[lastIndex] == '/' || path[lastIndex] == '\\');
    _current = lastIndex == 0 ? path : path.substring(0, lastIndex);
  }
  return _current!;
}