getFile method

Future<File?> getFile({
  1. required String fileName,
  2. String? filePath,
  3. FileMode? fileMode,
})

文件是否存在

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;
  }
}