path_type 1.0.0
path_type: ^1.0.0 copied to clipboard
A path type providing a type-safe and cross-platform path abstraction for managing and manipulating file paths, supporting both POSIX and Windows file systems.
Path Type #
path_type introduces a robust path type, Path, supporting POSIX and Windows file systems. Instead of using String, use Path to handle file paths in a type-safe manner with methods that will never throw an exception. Path can be used easily in-place of or with the path package. Path is also zero runtime cost as it is implemented as an extension type of String.
Usage #
import
import 'package:path_type/posix.dart'; // or
import 'package:path_type/windows.dart'; // or
import 'package:path_type/platform.dart'; // or, uses posix unless on windows
declaration
Path path1 = Path('/foo/bar');
Path path2 = 'bar'.asPath(); // or
Path path3 = './baz.txt' as Path; // or
convert back to string
Path path = Path('/foo/bar');
String string = path.string;
Create a path and perform basic operations:
import 'package:path_type/posix.dart';
void main() {
var path = Path('/foo/bar/baz.txt');
print('File name: ${path.fileName()}'); // Output: baz.txt
print('Extension: ${path.extension()}'); // Output: txt
print('Is absolute: ${path.isAbsolute()}'); // Output: true
var parent = path.parent();
if (parent.isSome()) {
print('Parent: ${parent.unwrap()}'); // Output: /foo/bar
}
var newPath = path.withExtension('md');
print('New path with extension: $newPath'); // Output: /foo/bar/baz.md
}
Get the components of a path:
void main() {
var path = Path('/foo/bar/baz.txt');
var components = path.components().toList();
for (var component in components) {
print(component); // Output: /, foo, bar, baz.txt
}
}
Retrieve all ancestors of a path:
void main() {
var path = Path('/foo/bar/baz.txt');
for (var ancestor in path.ancestors()) {
print(ancestor);
// Output:
// /foo/bar/baz.txt
// /foo/bar
// /foo
// /
}
}
Check if a path exists and get metadata:
void main() {
var path = Path('/foo/bar/baz.txt');
if (path.existsSync()) {
var metadata = path.metadataSync();
print('File size: ${metadata.size}');
print('Last modified: ${metadata.modified}');
} else {
print('Path does not exist.');
}
}
For more operations see the documentation
Comparison To The Path Package #
path_type has additional operations compared to the path package, see the documentation. Additionally, the path package only works with String, while Path can encapsulate the meaning through the type, providing clarity and preventing misuse. From an ergonomic perspective usage differs as such:
path_type
import 'package:path_type/posix.dart';
Path path1 = Path('/foo/bar');
Path path2 = Path('bar');
Path path3 = Path('./baz.txt');
Path path = path1.join(path2).join(path3);
print(path); // Output: /foo/bar/bar/./baz.txt
vs
path
import 'package:path/path.dart' as p;
String path1 = '/foo/bar';
String path2 = 'bar';
String path3 = './baz.txt';
p.Context posix = p.Context(style: p.Style.posix);
var x = posix.joinAll([path1, path2, path3]);
print(x); // Output: /foo/bar/bar/./baz.txt