getFile method
按平台规则定位文件,不存在则创建后返回;定位失败返回 null。
(注:返回的是 File 而非存在性 bool,存在性判断请用 fileExist。)
Implementation
Future<File?> getFile(
{required String fileName, String? filePath, FileMode? fileMode}) async {
// create({bool recursive: false})创建文件
// createSync({bool recursive: false}) 同步创建文件
// 可选命名参数:recursive 默认false,
// 若为true 则路径中有目录不存在时 会递归创建目录
// 若为false 则路径中的目录不存在时 会报错
try {
File? file;
if (Platform.isWindows) {
file =
await getLocalSupportFile(fileName: fileName, filePath: filePath);
} else if (Platform.isAndroid) {
final exFile = await getExternalStorageFile(
fileName: fileName, filePath: filePath);
if (exFile == null) {
file = await getLocalTemporaryFile(
fileName: fileName, filePath: filePath);
} else {
file = exFile;
}
} else {
file =
await getLocalDocumentFile(fileName: fileName, filePath: filePath);
}
if (file == null) {
return null;
}
final fileExists = await file.exists();
if (!fileExists) {
await file.create(recursive: true);
}
return file;
} catch (e) {
logger.e(tag: TAG, e.toString());
return null;
}
}