Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 3efca2f

Browse files
committed
getPathForShellItem throws on error
1 parent 9a6c7c5 commit 3efca2f

File tree

3 files changed

+48
-25
lines changed

3 files changed

+48
-25
lines changed

packages/file_selector/file_selector_windows/lib/src/file_selector_dart/dialog_wrapper.dart

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -181,34 +181,40 @@ class DialogWrapper {
181181
Pointer<Pointer<COMObject>> shellItemArrayPtr,
182182
Pointer<Uint32> shellItemCountPtr,
183183
Pointer<Pointer<COMObject>> shellItemPtr) {
184-
final List<String> files = <String>[];
185-
if (_isOpenDialog) {
186-
_lastResult = _dialogController.getResults(shellItemArrayPtr);
187-
if (!SUCCEEDED(_lastResult)) {
188-
return null;
189-
}
184+
try {
185+
final List<String> files = <String>[];
186+
int lastOperationResult;
187+
if (_isOpenDialog) {
188+
lastOperationResult = _dialogController.getResults(shellItemArrayPtr);
189+
if (!SUCCEEDED(lastOperationResult)) {
190+
throw WindowsException(lastOperationResult);
191+
}
190192

191-
final IShellItemArray shellItemResources =
192-
IShellItemArray(shellItemArrayPtr.cast());
193-
_lastResult = shellItemResources.getCount(shellItemCountPtr);
194-
if (!SUCCEEDED(_lastResult)) {
195-
return null;
196-
}
197-
for (int index = 0; index < shellItemCountPtr.value; index += 1) {
198-
shellItemResources.getItemAt(index, shellItemPtr);
193+
final IShellItemArray shellItemResources =
194+
IShellItemArray(shellItemArrayPtr.cast());
195+
lastOperationResult = shellItemResources.getCount(shellItemCountPtr);
196+
if (!SUCCEEDED(lastOperationResult)) {
197+
throw WindowsException(lastOperationResult);
198+
}
199+
for (int index = 0; index < shellItemCountPtr.value; index += 1) {
200+
shellItemResources.getItemAt(index, shellItemPtr);
201+
final IShellItem shellItem = IShellItem(shellItemPtr.cast());
202+
files.add(_shellWin32Api.getPathForShellItem(shellItem));
203+
_shellWin32Api.releaseShellItem(shellItem);
204+
}
205+
} else {
206+
lastOperationResult = _dialogController.getResult(shellItemPtr);
207+
if (!SUCCEEDED(lastOperationResult)) {
208+
throw WindowsException(lastOperationResult);
209+
}
199210
final IShellItem shellItem = IShellItem(shellItemPtr.cast());
200211
files.add(_shellWin32Api.getPathForShellItem(shellItem));
201212
_shellWin32Api.releaseShellItem(shellItem);
202213
}
203-
} else {
204-
_lastResult = _dialogController.getResult(shellItemPtr);
205-
if (!SUCCEEDED(_lastResult)) {
206-
return null;
207-
}
208-
final IShellItem shellItem = IShellItem(shellItemPtr.cast());
209-
files.add(_shellWin32Api.getPathForShellItem(shellItem));
210-
_shellWin32Api.releaseShellItem(shellItem);
214+
return files;
215+
} on WindowsException catch (ex) {
216+
_lastResult = ex.hr;
217+
return null;
211218
}
212-
return files;
213219
}
214220
}

packages/file_selector/file_selector_windows/lib/src/file_selector_dart/shell_win32_api.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ShellWin32Api {
3232
);
3333

3434
if (!SUCCEEDED(operationResult)) {
35-
return '';
35+
throw WindowsException(operationResult);
3636
}
3737
return ptrPath.value.toDartString();
3838
});

packages/file_selector/file_selector_windows/test/file_selector_dart/dialog_wrapper_test.dart

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,24 @@ void main() {
272272
verify(mockFileDialogController.getResult(any)).called(1);
273273
});
274274

275-
test('[DialogMode == Save] show should the selected directory for', () {
275+
test(
276+
'[DialogMode == Save] show should return null if cannot getPathForShellItem',
277+
() {
278+
final DialogWrapper dialogWrapperModeSave =
279+
DialogWrapper.withFakeDependencies(
280+
mockFileDialogController, DialogMode.save, mockShellWin32Api);
281+
const int parentWindow = 0;
282+
when(mockFileDialogController.show(parentWindow)).thenReturn(S_OK);
283+
when(mockFileDialogController.getResult(any)).thenReturn(S_OK);
284+
when(mockShellWin32Api.getPathForShellItem(any))
285+
.thenThrow(WindowsException(E_FAIL));
286+
287+
final List<String?>? result = dialogWrapperModeSave.show(parentWindow);
288+
289+
expect(result, null);
290+
});
291+
292+
test('[DialogMode == Save] show should return the file path', () {
276293
const String filePath = 'path/to/file.txt';
277294
final DialogWrapper dialogWrapperModeSave =
278295
DialogWrapper.withFakeDependencies(

0 commit comments

Comments
 (0)