Skip to content

Commit aa480ad

Browse files
BeMacizedfotiDim
authored andcommitted
[camera] Fix a disposed camera controller throwing an exception when being replaced in the preview widget. (flutter#4272)
1 parent 8cf2af7 commit aa480ad

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

packages/camera/camera/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.9.2+1
2+
3+
* Fixed camera controller throwing an exception when being replaced in the preview widget.
4+
15
## 0.9.2
26

37
* Added functions to pause and resume the camera preview.

packages/camera/camera/example/lib/main.dart

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,9 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
603603
}
604604

605605
void onNewCameraSelected(CameraDescription cameraDescription) async {
606-
final previousCameraController = controller;
606+
if (controller != null) {
607+
await controller!.dispose();
608+
}
607609

608610
final CameraController cameraController = CameraController(
609611
cameraDescription,
@@ -614,10 +616,6 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
614616

615617
controller = cameraController;
616618

617-
if (mounted) {
618-
setState(() {});
619-
}
620-
621619
// If the controller is updated then update the UI.
622620
cameraController.addListener(() {
623621
if (mounted) setState(() {});
@@ -650,8 +648,6 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
650648
if (mounted) {
651649
setState(() {});
652650
}
653-
654-
await previousCameraController?.dispose();
655651
}
656652

657653
void onTakePictureButtonPressed() {

packages/camera/camera/lib/src/camera_controller.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,4 +824,14 @@ class CameraController extends ValueNotifier<CameraValue> {
824824
);
825825
}
826826
}
827+
828+
@override
829+
void removeListener(VoidCallback listener) {
830+
// Prevent ValueListenableBuilder in CameraPreview widget from causing an
831+
// exception to be thrown by attempting to remove its own listener after
832+
// the controller has already been disposed.
833+
if (!_isDisposed) {
834+
super.removeListener(listener);
835+
}
836+
}
827837
}

packages/camera/camera/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: A Flutter plugin for getting information about and controlling the
44
and streaming image buffers to dart.
55
repository: https://github.com/flutter/plugins/tree/master/packages/camera/camera
66
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
7-
version: 0.9.2
7+
version: 0.9.2+1
88

99
environment:
1010
sdk: ">=2.12.0 <3.0.0"

0 commit comments

Comments
 (0)