Skip to content

[camera_android_camerax] Fix setFocusMode(auto) crash when there is no auto-focus point#11884

Open
motucraft wants to merge 2 commits into
flutter:mainfrom
motucraft:fix/camera-setfocusmode-auto-empty-af-points
Open

[camera_android_camerax] Fix setFocusMode(auto) crash when there is no auto-focus point#11884
motucraft wants to merge 2 commits into
flutter:mainfrom
motucraft:fix/camera-setfocusmode-auto-empty-af-points

Conversation

@motucraft

@motucraft motucraft commented Jun 11, 2026

Copy link
Copy Markdown

Adds the missing isEmpty guard to the FocusMode.auto branch of AndroidCameraCameraX.setFocusMode, which read meteringPointsAf.first unconditionally and threw StateError: Bad state: No element when the current focus-and-metering action had no auto-focus point.

The FocusMode.locked branch already guards this; the auto branch did not.

Fixes flutter/flutter#187782

Pre-Review Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. 2

@motucraft motucraft changed the title [camera_android_camerax] Fixes setFocusMode(auto) crash when there is no auto-focus point [camera_android_camerax] Fix setFocusMode(auto) crash when there is no auto-focus point Jun 11, 2026
@motucraft motucraft marked this pull request as ready for review June 11, 2026 02:42

@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 StateError in setFocusMode(FocusMode.auto) when the current focus and metering action has no auto-focus points. The implementation safely retrieves the list of auto-focus points and checks if it is empty before accessing the first element. A unit test has been added to verify this behavior, and the package version has been bumped to 0.7.2+2. There are no review comments to address.

@reidbaker reidbaker requested a review from camsim99 June 16, 2026 19:31
@reidbaker reidbaker added platform-android triage-android Should be looked at in Android triage labels Jun 16, 2026
@motucraft motucraft force-pushed the fix/camera-setfocusmode-auto-empty-af-points branch from 9fce66d to deaed81 Compare June 20, 2026 06:01

@camsim99 camsim99 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.

LGTM! Thank you for the extremely well-written bug and a straightforward fix :)

@camsim99 camsim99 added the CICD Run CI/CD label Jun 22, 2026
@camsim99 camsim99 requested a review from reidbaker June 22, 2026 16:28
final MeteringPoint? unLockedFocusPoint = _defaultFocusPointLocked
final List<MeteringPoint> possibleCurrentAfPoints =
currentFocusMeteringAction?.meteringPointsAf ?? [];
final MeteringPoint? unLockedFocusPoint =

@reidbaker reidbaker Jun 22, 2026

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.

This does not make sense to me. let me rewrite the turnary to an if statement to make it a bit easier to read.

if(_defaultFocusPointLocked || possibleCurrentAfPoints.isEmpty) {
   return null
} else {
   return possibleCurrentAfPoints.first;
}

I believe this code will throw a state error if _defaultFocusPointLocked == true and possibleCurrentAfPoints is an empty list. Which looks like the error you are claiming to fix.

Did I make a mistake or is there something I am misunderstanding?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@reidbaker

Thanks for the review. I think this should not throw. With this condition, possibleCurrentAfPoints.first is only evaluated when _defaultFocusPointLocked is false and possibleCurrentAfPoints is not empty. If _defaultFocusPointLocked is true or the list is empty, the expression returns null.

Please let me know if I’m missing something.

@reidbaker reidbaker Jun 23, 2026

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.

facepalm, yes sorry reviewing code tired will do that. Thank you for responding with grace.

…ra-setfocusmode-auto-empty-af-points

# Conflicts:
#	packages/camera/camera_android_camerax/CHANGELOG.md
#	packages/camera/camera_android_camerax/pubspec.yaml
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Jul 2, 2026
@motucraft

Copy link
Copy Markdown
Author

This PR became conflicted while waiting after approval. I’ve merged the latest main and resolved the conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p: camera triage-android Should be looked at in Android triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[camera] setFocusMode(FocusMode.auto) throws "StateError: Bad state: No element" when the focus-metering action has no AF points

3 participants