getDirectoryPath method

  1. @override
Future<String?> getDirectoryPath({
  1. String? dialogTitle,
  2. bool lockParentWindow = false,
  3. String? initialDirectory,
})
override

Selects a directory and returns its absolute path.

On Android, this requires to be running on SDK 21 or above, else won't work. Note: Some Android paths are protected, hence can't be accessed and will return / instead.

dialogTitle can be set to display a custom title on desktop platforms. It will be ignored on Web & IO.

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.

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

Returns a Future<String?> which resolves to the absolute path of the selected directory, if the user selected a directory. Returns null if the user aborted the dialog or if the folder path couldn't be resolved.

Note: on Windows, throws a WindowsException with a detailed error message, if the dialog could not be instantiated or the dialog result could not be interpreted. Note: Some Android paths are protected, hence can't be accessed and will return / instead.

Implementation

@override
Future<String?> getDirectoryPath({
  String? dialogTitle,
  bool lockParentWindow = false,
  String? initialDirectory,
}) async {
  final String executable = await isExecutableOnPath('osascript');
  final List<String> arguments = generateCommandLineArguments(
    escapeDialogTitle(dialogTitle ?? defaultDialogTitle),
    initialDirectory: initialDirectory ?? '',
    pickDirectory: true,
  );

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

  return resultStringToFilePaths(directorySelectionResult).first;
}