Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
fix: disposed CameraController error thrown when changing a camera (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bselwe authored Aug 13, 2021
1 parent b1c65df commit c8570fc
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/camera/camera/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -594,17 +594,21 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
}

void onNewCameraSelected(CameraDescription cameraDescription) async {
if (controller != null) {
await controller!.dispose();
}
final previousCameraController = controller;

final CameraController cameraController = CameraController(
cameraDescription,
ResolutionPreset.medium,
enableAudio: enableAudio,
imageFormatGroup: ImageFormatGroup.jpeg,
);

controller = cameraController;

if (mounted) {
setState(() {});
}

// If the controller is updated then update the UI.
cameraController.addListener(() {
if (mounted) setState(() {});
Expand Down Expand Up @@ -637,6 +641,8 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
if (mounted) {
setState(() {});
}

await previousCameraController?.dispose();
}

void onTakePictureButtonPressed() {
Expand Down

0 comments on commit c8570fc

Please sign in to comment.