buildPathInDoc static method
构建Documents下的路径
示例代码:
// 构建目录路径
String path1 = await PPath.buildPathInDoc(subPath: 'dir1'); // 返回 Documents/dir1
// 构建文件路径
String path2 = await PPath.buildPathInDoc(fileName: 'file.txt'); // 返回 Documents/file.txt
// 构建带子目录的文件路径
String path3 = await PPath.buildPathInDoc(subPath: 'dir1', fileName: 'file.txt'); // 返回 Documents/dir1/file.txt
String path4 = await PPath.buildPathInDoc(subPath: 'dir1/dir2', fileName: 'file.txt'); // 返回 Documents/dir1/dir2/file.txt
Implementation
static Future<String> buildPathInDoc({
String subPath = '',
String fileName = '',
}) async {
var fullPath = (await PPath.appDocumentDir()).path;
// 如果 subPath 不为空,则拼接 subPath 并确保目录存在
if (subPath.isNotEmpty) {
fullPath = dart_path.join(fullPath, subPath);
final parentDir = Directory(fullPath);
if (!await parentDir.exists()) {
await parentDir.create(recursive: true);
}
}
// 如果 fileName 不为空,则拼接 fileName 到路径
if (fileName.isNotEmpty) {
fullPath = dart_path.join(fullPath, fileName);
}
return fullPath;
}