Skip to content

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

Merged
auto-submit[bot] merged 3 commits into
flutter:mainfrom
motucraft:fix/camera-setfocusmode-auto-empty-af-points
Jul 7, 2026
Merged

[camera_android_camerax] Fix setFocusMode(auto) crash when there is no auto-focus point#11884
auto-submit[bot] merged 3 commits into
flutter:mainfrom
motucraft:fix/camera-setfocusmode-auto-empty-af-points

Conversation

@motucraft

@motucraftmotucraft commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

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.

Fixesflutter/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

@motucraftmotucraft 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 pointJun 11, 2026
@motucraft
motucraft marked this pull request as ready for review June 11, 2026 02:42

@gemini-code-assistgemini-code-assistBot 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 camsim99June 16, 2026 19:31
@reidbakerreidbaker added platform-android triage-android Should be looked at in Android triage labels Jun 16, 2026
@motucraft
motucraftforce-pushed the fix/camera-setfocusmode-auto-empty-af-points branch from 9fce66d to deaed81CompareJune 20, 2026 06:01

@camsim99camsim99 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 :)

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

@reidbakerreidbakerJun 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
ContributorAuthor

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.

@reidbakerreidbakerJun 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-dashboardflutter-dashboardBot removed the CICD Run CI/CD label Jul 2, 2026
@motucraft

Copy link
Copy Markdown
ContributorAuthor

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

@stuartmorgan-g

Copy link
Copy Markdown
Collaborator

It looks like autosubmit wasn't added with the final approval for some reason.

@stuartmorgan-gstuartmorgan-g added autosubmit Merge PR when tree becomes green via auto submit App CICD Run CI/CD labels Jul 7, 2026
@auto-submit
auto-submitBot merged commit 92525f5 into flutter:mainJul 7, 2026
88 checks passed
pullBot pushed a commit to soloinovator/flutter that referenced this pull request Jul 7, 2026
…r#189069)
flutter/packages@fc00ceb...92525f5
2026-07-07 35750184+motucraft@users.noreply.github.com
[camera_android_camerax] Fix setFocusMode(auto) crash when there is no
auto-focus point (flutter/packages#11884)
2026-07-06 bolling.ludwig@gmail.com [camera_android,
camera_android_camerax, camera_avfoundation] Add setJpegImageQuality
(flutter/packages#11616)
If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC flutter-ecosystem@google.com on the revert to ensure that a
human
is aware of the problem.
To file a bug in Flutter:
https://github.com/flutter/flutter/issues/new/choose
To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622
Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
kalyujniy pushed a commit to brickit-app/camera that referenced this pull request Jul 8, 2026
…o auto-focus point (flutter#11884)
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.
Fixesflutter/flutter#187782
**Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). 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.
[^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.
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmitMerge PR when tree becomes green via auto submit AppCICDRun CI/CDp: cameratriage-androidShould 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

4 participants

@motucraft@stuartmorgan-g@reidbaker@camsim99