You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit fd6ef13
Browse filesBrowse the repository at this point in the historyBrowse files
[file_selector_android] Surface file-copy failures to Dart instead of crashing (flutter#12107)
`file_selector_android` crashes when a selected file can't be copied to a readable location.
`FileSelectorApiImpl.toFileResponse` catches `IOException`/`SecurityException` from `FileUtils.getPathFromCopyOfFileFromUri` and sets the path to `null`, but then still builds a `FileResponse` with that null `path`. Since `path` is non-null in the Pigeon-generated `FileResponse`, this throws — a `NullPointerException` since the move to the Kotlin Pigeon generator (the issue reported it as an `IllegalStateException` on the older Java generator) — so the app crashes instead of surfacing an error.
When no path can be produced, `toFileResponse` now returns `null`. The callers already handle that by completing the result with an error (the same `completeWithError("Failed to read file: …")` path they use elsewhere when a file can't be read), so the failure now reaches Dart as a `PlatformException` instead of crashing the app.
I kept this scoped to the crash. The issue also suggests moving these branches onto the typed `FileSelectorNativeException` mechanism (like flutter#8184); that would need a Pigeon regeneration and a Dart-facing API change, so I left it out here — happy to do it as a follow-up if that's the preferred direction.
The existing test that documented the crash (marked "Remove when fixed") now asserts the failure is surfaced, plus a matching test for the single-file `openFile` path.
Fixesflutter#159568
## Pre-Review Checklist
Copy file name to clipboardExpand all lines: packages/file_selector/file_selector_android/android/src/main/java/dev/flutter/packages/file_selector_android/FileSelectorApiImpl.java
+10Lines changed: 10 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -383,6 +383,16 @@ FileResponse toFileResponse(@NonNull Uri uri) {
383
383
e.getMessage() == null ? "" : e.getMessage());
384
384
}
385
385
386
+
if (uriPath == null) {
387
+
// `getPathFromCopyOfFileFromUri` can fail to produce a path: either by
388
+
// throwing (handled above by returning a null `uriPath`) or by returning
389
+
// null directly. Return null so the caller surfaces the failure to Dart,
390
+
// instead of building a `FileResponse` with a null `path`, which the
391
+
// non-null field rejects at runtime.
392
+
// See https://github.com/flutter/flutter/issues/159568.
Copy file name to clipboardExpand all lines: packages/file_selector/file_selector_android/android/src/test/java/dev/flutter/packages/file_selector_android/FileSelectorAndroidPluginTest.java
@@ -214,36 +216,25 @@ public void openFilesReturnsSuccessfully() throws FileNotFoundException {
214
216
}
215
217
}
216
218
217
-
// This test was created when error handling was moved from FileUtils.java to
218
-
// FileSelectorApiImpl.java
219
-
// in https://github.com/flutter/packages/pull/8184, so as to maintain the existing test.
220
-
// The behavior is actually an error case and should be fixed,
0 commit comments