path_type

Last updated:

0 purchases

path_type Image
path_type Images
Add to Cart

Description:

path type

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
copied to clipboard
declaration
Path path1 = Path('/foo/bar');
Path path2 = 'bar'.asPath(); // or
Path path3 = './baz.txt' as Path; // or
copied to clipboard
convert back to string
Path path = Path('/foo/bar');
String string = path.string;
copied to clipboard
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
}
copied to clipboard
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
}
}
copied to clipboard
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
// /
}
}
copied to clipboard
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.');
}
}
copied to clipboard
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
copied to clipboard
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
copied to clipboard

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.