saveFile method

  1. @override
Future<String?> saveFile({
  1. String? dialogTitle,
  2. String? fileName,
  3. String? initialDirectory,
  4. FileType type = FileType.any,
  5. List<String>? allowedExtensions,
  6. Uint8List? bytes,
  7. bool lockParentWindow = false,
})
override

Opens a save file dialog which lets the user select a file path and a file name to save a file.

For mobile platforms, this function will save file with bytes to return a path.

For desktop platforms (Linux, macOS & Windows),This function does not actually save a file. It only opens the dialog to let the user choose a location and file name. This function only returns the path to this (non-existing) file.

dialogTitle can be set to display a custom title on desktop platforms.

fileName can be set to a non-empty string to provide a default file name. Throws an IllegalCharacterInFileNameException under Windows if the given fileName contains forbidden characters.

initialDirectory can be optionally set to an absolute path to specify where the dialog should open. Only supported on Linux, macOS, and Windows.

The file type filter type defaults to FileType.any. Optionally, allowedExtensions might be provided (e.g. [pdf, svg, jpg].). Both parameters are just a proposal to the user as the save file dialog does not enforce these restrictions.

If lockParentWindow is set, the child window (file picker window) will stay in front of the Flutter window until it is closed (like a modal window). This parameter works only on Windows desktop.

Returns null if aborted. Returns a Future<String?> which resolves to the absolute path of the selected file, if the user selected a file.

Implementation

@override
Future<String?> saveFile({
  String? dialogTitle,
  String? fileName,
  String? initialDirectory,
  FileType type = FileType.any,
  List<String>? allowedExtensions,
  Uint8List? bytes,
  bool lockParentWindow = false,
}) async {
  final String executable = await isExecutableOnPath('osascript');
  final String fileFilter = fileTypeToFileFilter(
    type,
    allowedExtensions,
  );
  final List<String> arguments = generateCommandLineArguments(
    escapeDialogTitle(dialogTitle ?? defaultDialogTitle),
    fileFilter: fileFilter,
    fileName: fileName ?? '',
    initialDirectory: initialDirectory ?? '',
    saveFile: true,
  );

  final String? saveFileResult = await runExecutableWithArguments(
    executable,
    arguments,
  );
  if (saveFileResult == null) {
    return null;
  }

  return resultStringToFilePaths(saveFileResult).first;
}