This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[camera] Improve the camera example with controllers initializations #5748
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
436f5b4
⚡️ Only update the controller if it's initialized successfully
AlexV525 23a03f9
🎨 Format
AlexV525 264dab6
🔖 0.9.5+1
AlexV525 89ff144
🔥 Remove condition and add comments
AlexV525 dfb46c8
💡 Update comment
AlexV525 d9c2e61
🎨 Trailing comma
AlexV525 4b5862d
Merge branch 'main' into improve-camera-example
AlexV525 764d0b4
🚑️ Fix reverted web condition
AlexV525 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -651,39 +651,45 @@ class _CameraExampleHomeState extends State<CameraExampleHome> | |
imageFormatGroup: ImageFormatGroup.jpeg, | ||
); | ||
|
||
controller = cameraController; | ||
|
||
// If the controller is updated then update the UI. | ||
cameraController.addListener(() { | ||
if (mounted) { | ||
setState(() {}); | ||
} | ||
if (cameraController.value.hasError) { | ||
showInSnackBar( | ||
'Camera error ${cameraController.value.errorDescription}'); | ||
'Camera error ${cameraController.value.errorDescription}', | ||
); | ||
} | ||
}); | ||
|
||
try { | ||
await cameraController.initialize(); | ||
await Future.wait(<Future<Object?>>[ | ||
// The exposure mode is currently not supported on the web. | ||
...!kIsWeb | ||
? <Future<Object?>>[ | ||
cameraController.getMinExposureOffset().then( | ||
(double value) => _minAvailableExposureOffset = value), | ||
cameraController | ||
.getMaxExposureOffset() | ||
.then((double value) => _maxAvailableExposureOffset = value) | ||
] | ||
: <Future<Object?>>[], | ||
if (!kIsWeb) ...<Future<Object?>>[ | ||
cameraController | ||
.getMinExposureOffset() | ||
.then((double value) => _minAvailableExposureOffset = value), | ||
cameraController | ||
.getMaxExposureOffset() | ||
.then((double value) => _maxAvailableExposureOffset = value), | ||
], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this just re-formatting? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. |
||
cameraController | ||
.getMaxZoomLevel() | ||
.then((double value) => _maxAvailableZoom = value), | ||
cameraController | ||
.getMinZoomLevel() | ||
.then((double value) => _minAvailableZoom = value), | ||
]); | ||
// `controller` needs to be set until it's fully initialized to avoid a | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this supposed to say "needs to be unset"? |
||
// race condition when the lifecycle callback is being called and creates | ||
// a new `controller`. This happens when the camera permission dialog | ||
// dismisses at the first run, which triggers `didChangeAppLifecycleState` | ||
// that disposes and re-creates the controller. Switching controllers in a | ||
// short circumstance might cause the camera preview flashes or make the | ||
// state hard to manage. | ||
controller = cameraController; | ||
AlexV525 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} on CameraException catch (e) { | ||
switch (e.code) { | ||
case 'CameraAccessDenied': | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this change, wouldn't two calls to
onNewCameraSelected
in rapid succession have a race where a controller would be replaced without being disposed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is not covered in this PR yet. Let me know if the example can be updated/refactored for better architecture or for test purposes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, anything we can do to make the example better is a win. We should try to make sure that testability changes are minimally intrusive when possible so they don't distract from understanding of the example itself.