[camera_android_camerax] Fix setFocusMode(auto) crash when there is no auto-focus point#11884
[camera_android_camerax] Fix setFocusMode(auto) crash when there is no auto-focus point#11884motucraft wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
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.
… no auto-focus point
9fce66d to
deaed81
Compare
camsim99
left a comment
There was a problem hiding this comment.
LGTM! Thank you for the extremely well-written bug and a straightforward fix :)
| final MeteringPoint? unLockedFocusPoint = _defaultFocusPointLocked | ||
| final List<MeteringPoint> possibleCurrentAfPoints = | ||
| currentFocusMeteringAction?.meteringPointsAf ?? []; | ||
| final MeteringPoint? unLockedFocusPoint = |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
|
This PR became conflicted while waiting after approval. I’ve merged the latest main and resolved the conflicts. |
Adds the missing
isEmptyguard to theFocusMode.autobranch ofAndroidCameraCameraX.setFocusMode, which readmeteringPointsAf.firstunconditionally and threwStateError: Bad state: No elementwhen the current focus-and-metering action had no auto-focus point.The
FocusMode.lockedbranch already guards this; theautobranch did not.Fixes flutter/flutter#187782
Pre-Review Checklist
[shared_preferences]///).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-assistbot 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
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