Skip to content

[file_selector_android] Surface file-copy failures to Dart instead of crashing#12107

Open
tonytonycoder11 wants to merge 2 commits into
flutter:mainfrom
tonytonycoder11:file-selector-null-path-crash
Open

[file_selector_android] Surface file-copy failures to Dart instead of crashing#12107
tonytonycoder11 wants to merge 2 commits into
flutter:mainfrom
tonytonycoder11:file-selector-null-path-crash

Conversation

@tonytonycoder11

@tonytonycoder11 tonytonycoder11 commented Jul 5, 2026

Copy link
Copy Markdown

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 #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.

Fixes flutter/flutter#159568

Pre-Review Checklist

… crashing

Return null from FileSelectorApiImpl.toFileResponse when a file cannot be
resolved to a path, so the existing error path surfaces a PlatformException
to Dart instead of building a FileResponse with a null path (which crashes
the non-null Pigeon field).

Fixes flutter/flutter#159568
@google-cla

google-cla Bot commented Jul 5, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes a crash in the Android file selector implementation when a selected file cannot be copied to a readable location (such as during a SecurityException). It updates FileSelectorApiImpl to return null when uriPath is null, allowing the failure to be surfaced to Dart, and updates the corresponding tests to verify this behavior. The review feedback suggests using primitive boolean[] arrays instead of boxed Boolean[] arrays in the tests to prevent potential NullPointerExceptions during unboxing in assertions.

Avoids a NullPointerException on unboxing if a callback is never invoked; a
primitive boolean[] defaults to false, so a missing callback surfaces as a
clear assertion failure instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[file_selector_android] Attempting to return null paths from FileUtils.java results in an IllegalStateException

1 participant