getDirectoryPaths function
Opens a native directory selection dialog allowing multiple folders to be chosen.
Throws UnsupportedError on Android (where Storage Access Framework only supports single-tree selection via getDirectoryPath / openDirectory) and on Web (where browsers do not expose raw local directory paths). Returns an empty list if the user cancels the dialog.
Implementation
Future<List<String>> getDirectoryPaths({
String? initialDirectory,
String? confirmButtonText,
bool? canCreateDirectories,
}) async {
if (isWebRuntime) {
throw UnsupportedError(
'Browsers do not expose raw local directory paths on Web.',
);
}
if (effectiveNativePlatform == TargetPlatform.android) {
throw UnsupportedError(
'Android Storage Access Framework (ACTION_OPEN_DOCUMENT_TREE) only '
'supports selecting a single directory. Use getDirectoryPath() or '
'openDirectory() instead.',
);
}
final raw = await DesktopDrop.channel.invokeMethod<List<Object?>>(
'getDirectoryPaths',
<String, dynamic>{
'initialDirectory': initialDirectory,
'confirmButtonText': confirmButtonText,
if (canCreateDirectories != null)
'canCreateDirectories': canCreateDirectories,
},
);
if (raw == null || raw.isEmpty) {
return const <String>[];
}
return raw
.whereType<String>()
.where((String path) => path.isNotEmpty)
.toList(growable: false);
}