New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[camera][ios] : Switching between front and back camera throws CameraException(Disposed CameraController, buildPreview() was called on a disposed CameraController.)
exception.
#90116
Comments
@carman247 |
@darshankawar, it could be the same issue. So far I'm unable to reproduce without forcing it. It hasn't occurred on any Android devices yet, only iOS ... Also, it seems like it's only occurring since upgrading to 0.9.2+2 |
@carman247 Thanks for the update. |
@darshankawar This definitely seems to have something to do with
This is how mine is setup, basically the same as the camera example.
|
@carman247 |
@darshankawar I'm pretty sure I'm implementing that although I'm not really sure what's it doing apart from adding the observer in Can you explain how the I'm also using
|
@carman247 Thanks. I see what you are saying. I tried the official plugin example on latest master and stable and ran it on iOS. It doesn't replicate immediately and we have to keep switching between front and back camera few times, that leads to below exception: console error log
stable, master flutter doctor -v
|
CameraException(Disposed CameraController, buildPreview() was called on a disposed CameraController.)
exception.
@darshankawar no problem! |
Hi, I'm not sure if this is helpful at all but I think I've managed to trigger this as well and have potentially found a fix for my situation. Previously my widgets we checking
I noticed that
I am also calling Moving the
|
Thanks. I thought it seemed to happen less in release mode, but not sure |
This is definitely an issue for me still ... I'm not quite sure what the problem is, it only seems to happen on iOS and only occurs when the app enters Not only can I reproduce in my app but also in the example app provided. Also, is anyone able to explain the significance/importance of creating a copy of the current
I find this causes confusion and don't really understand why the original |
Just adding to this issue. I'm getting this exception in one of my app. The first screen allow the user to scan with the This exception is thrown once in a while in Android and iOS devices. Flutter is latest stable and my current package versions are:
|
I have the same problem on phisical Motorola g8 with Android 11. My code is pretty much copy-paste from example: https://github.com/danPyk/flutter_tensor_flow/blob/master/lib/obj_det/front/camera.dart Error occurs when Im putting app to sleep (state: AppLifecycleState.paused) by using phisical button and also when I'm moving back to app. Error log: https://pastebin.com/Yqw2QYgy |
It seems that the CameraPreview needs to be completely released from the Widget tree before disposing the CameraController. Use WidgetsBinding.instance.addPostFrameCallback to wait for the frame to update after the setState call before calling dispose() on the CameraController. final cameraController = _cameraController;
_cameraController = null;
if (mounted) {
setState(() {});
// Wait for the post frame callback.
final completerPostFrameCallback = Completer<Duration>();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
completerPostFrameCallback.complete(timeStamp);
});
await completerPostFrameCallback.future;
}
return cameraController!.dispose(); Whole Dart code. import 'dart:async';
import 'package:camera/camera.dart';
import 'package:flutter/cupertino.dart';
void main() {
runApp(CameraTest());
}
class CameraTest extends StatefulWidget {
@override
State createState() => CameraTestState();
}
class CameraTestState extends State<CameraTest> with WidgetsBindingObserver {
CameraController? _cameraController;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_initCameraController();
}
@override
void dispose() {
_disposeCameraController();
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (AppLifecycleState.paused == state) {
_disposeCameraController();
} else {
if (_cameraController == null) {
_initCameraController();
}
}
}
@override
Widget build(BuildContext context) {
return Directionality(
textDirection: TextDirection.ltr,
child: Stack(
alignment: Alignment.center,
children: [
if (_cameraController?.value.isInitialized == true)
CameraPreview(_cameraController!),
],
),
);
}
Future<void> _initCameraController() async {
if (_cameraController != null) {
return Future.value();
}
final List<CameraDescription> cameras = await availableCameras();
final descriptionIndex = cameras.indexWhere(
(element) => element.lensDirection == CameraLensDirection.back
);
final CameraDescription description = cameras.elementAt(descriptionIndex);
_cameraController = CameraController(
description,
ResolutionPreset.medium,
enableAudio: false,
);
await _cameraController!.initialize();
if (mounted) {
setState(() {});
}
}
Future<void> _disposeCameraController() async {
if (_cameraController == null) {
return Future.value();
}
final cameraController = _cameraController;
_cameraController = null;
if (mounted) {
setState(() {});
// Wait for the post frame callback.
final completerPostFrameCallback = Completer<Duration>();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
completerPostFrameCallback.complete(timeStamp);
});
await completerPostFrameCallback.future;
}
return cameraController!.dispose();
}
} |
@lynrin the setState call from _disposeCameraController causes a
|
I don't know how this is happening yet, and it doesn't seem to be a major problem, but for some reason I'm getting reports of "buildPreview() was called on a disposed CameraController"
CameraException(Disposed CameraController, buildPreview() was called on a disposed CameraController.).
I can see there is a "debugCheckIsDisposed" function, which returns a void, but maybe it would be useful to either have that return a bool or set "isInitialised" to false after dispose has been called.
I'm using camera 0.9.2+2 and it's basically the same setup as the example.
log
The text was updated successfully, but these errors were encountered: