createPathToFile static method

Future<String> createPathToFile({
  1. required LocalDir localDir,
  2. String? subPath,
  3. required String fileName,
  4. bool validateFileName = true,
})

Allows for the easy creation of a path string containing a file at the end.

createPathToFile(
  localDir: LocalDir.tempDir,
  subPath: "/images",
  fileName: "smile.jpg"
) == "/path/to/tmp/images/smile.jpg"

Throws a FormatException if subPath is not a valid path or if fileName is not a valid file name. See createPath and isValidFileName for examples of invalid inputs.

Implementation

static Future<String> createPathToFile({
  required LocalDir localDir,
  String? subPath,
  required String fileName,
  bool validateFileName = true,
}) async {
  String path;
  final String fileNameSterile = sterilizeFileName(fileName);

  if (subPath != null) {
    path = await createPath(localDir: localDir, subPath: subPath);
  } else {
    path = await fetchPath(localDir);
  }

  // Check for valid file name.
  if (validateFileName) {
    if (!isValidFileName(fileName)) {
      throw FormatException("$fileName is not a valid file name!");
    }
  }

  return "$path${Platform.pathSeparator}$fileNameSterile";
}