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. Throws UnsupportedError on macOS if bytes are provided.

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.

The User Selected File Read/Write entitlement is required on macOS.

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. On macOS the home directory shortcut (~/) is not necessary and passing it will be ignored. On macOS if the initialDirectory is invalid the user directory or previously valid directory will be used.

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}) {
  if (Platform.isIOS || Platform.isAndroid) {
    if (bytes == null) {
      throw ArgumentError(
          'Bytes are required on Android & iOS when saving a file.');
    }

    return _channel.invokeMethod("save", {
      "fileName": fileName,
      "fileType": type.name,
      "initialDirectory": initialDirectory,
      "allowedExtensions": allowedExtensions,
      "bytes": bytes,
    });
  }
  return super.saveFile(
    dialogTitle: dialogTitle,
    fileName: fileName,
    initialDirectory: initialDirectory,
    type: type,
    allowedExtensions: allowedExtensions,
    bytes: bytes,
    lockParentWindow: lockParentWindow,
  );
}