Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
⚡️ Only update the controller if it's initialized successfully
  • Loading branch information
AlexV525 committed May 15, 2022
commit 436f5b4c4868315c1307e99d676c4564fad6a599
9 changes: 7 additions & 2 deletions packages/camera/camera/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,6 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
imageFormatGroup: ImageFormatGroup.jpeg,
);

controller = cameraController;
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Contributor

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.


// If the controller is updated then update the UI.
cameraController.addListener(() {
if (mounted) {
Expand All @@ -666,6 +664,12 @@ class _CameraExampleHomeState extends State<CameraExampleHome>

try {
await cameraController.initialize();
if (cameraController.value.hasError) {
showInSnackBar(
'Camera error ${cameraController.value.errorDescription}',
);
return;
}
await Future.wait(<Future<Object?>>[
// The exposure mode is currently not supported on the web.
...!kIsWeb
Expand All @@ -684,6 +688,7 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
.getMinZoomLevel()
.then((double value) => _minAvailableZoom = value),
]);
controller = cameraController;
} on CameraException catch (e) {
switch (e.code) {
case 'CameraAccessDenied':
Expand Down