relative method

String relative(
  1. String path, {
  2. String? from,
})

Attempts to convert path to an equivalent relative path from the current directory.

// Given current directory is /root/path:
p.relative('/root/path/a/b.dart'); // -> 'a/b.dart'
p.relative('/root/other.dart'); // -> '../other.dart'

If the from argument is passed, path is made relative to that instead.

p.relative('/root/path/a/b.dart', from: '/root/path'); // -> 'a/b.dart'
p.relative('/root/other.dart', from: '/root/path');
  // -> '../other.dart'

If path and/or from are relative paths, they are assumed to be relative to the current directory.

Since there is no relative path from one drive letter to another on Windows, or from one hostname to another for URLs, this will return an absolute path in those cases.

// Windows
p.relative(r'D:\other', from: r'C:\home'); // -> 'D:\other'

// URL
p.relative('https://dart.dev', from: 'https://pub.dev');
  // -> 'https://dart.dev'

Implementation

String relative(String path, {String? from}) => p.context.relative(path, from: from);