path_type 3.0.1
path_type: ^3.0.1 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 path types for working with file paths in a structured and type-safe manner, supporting Unix (POSIX) and Windows file systems. All Path types are extension types of string, so they are zero runtime cost.
Types #
Path - A platform
dependent path type - uses WindowPath
on windows and UnixPath
on all other platforms.
UnixPath - A Unix path type.
WindowsPath - A Windows path type.
Basic Operations #
Create a path and perform basic operations:
import 'package:path_type/path_type.dart';
void main() {
var path = UnixPath('/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 != null) {
print('Parent: $parent'); // Output: /foo/bar
}
var newPath = path.withExtension('md');
print('New path with extension: $newPath'); // Output: /foo/bar/baz.md
}
copied to clipboard
Extracting Components #
Get the components of a path:
void main() {
var path = UnixPath('/foo/bar/baz.txt');
var components = path.components().toList();
for (var component in components) {
print(component); // Output: /, foo, bar, baz.txt
}
}
copied to clipboard
Ancestors #
Retrieve all ancestors of a path:
void main() {
var path = UnixPath('/foo/bar/baz.txt');
for (var ancestor in path.ancestors()) {
print(ancestor);
// Output:
// /foo/bar/baz.txt
// /foo/bar
// /foo
// /
}
}
copied to clipboard
File System Interaction #
Check if a path exists and get metadata:
void main() {
var path = UnixPath('/foo/bar/baz.txt');
if (path.existsSync()) {
var metadata = path.metadataSync().unwrap();
print('File size: ${metadata.size}');
print('Last modified: ${metadata.modified}');
} else {
print('Path does not exist.');
}
}
copied to clipboard