“Disposed CameraController” “StopImageStream() was called on a disposed…
- User
“Disposed CameraController” “StopImageStream() was called on a disposed CameraController” Wie fixe ich das?
import 'dart:async';
import 'package:code_scan/code_scan.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/patient_app_localizations.dart'; import 'package:patient_app/models/scan_result.dart'; import 'package:patient_app/theme/patient_app_colors.dart'; import 'package:patient_app/widgets/patient_app_back_button.dart';
typedef QrScanResultCallback = Future Function(ScanResult);
// see https://pub.dev/packages/code_scan/example class QrScanner extends StatefulWidget { const QrScanner( {Key? key, required this.callback, required this.backButtonCallback}) : super(key: key);
final QrScanResultCallback callback; final BackButtonCallback backButtonCallback;
@override State createState() => _QrScannerState(); }
class _QrScannerState extends State with WidgetsBindingObserver { final GlobalKey qrKey = GlobalKey(debugLabel: 'QR'); late AppLocalizations _l10n;
// prevent over processing bool processingInProgress = false;
@override void didChangeDependencies() { _l10n = AppLocalizations.of(context)!; super.didChangeDependencies(); }
@override Widget build(BuildContext context) { return Container( color: AppColors.opascaBlue, child: _buildQrView(), ); }
Widget buildQrView() { return CodeScanner( key: qrKey, formats: const [BarcodeFormat.qrCode], scanInterval: const Duration(milliseconds: 500), onScan: (code, , listener) { if (code != null) { if (processingInProgress == false) { processingInProgress = true; widget.callback(ScanResult(code)).then((isValidCode) { if (mounted && !isValidCode) { processingInProgress = false; } }); } } }, onAccessDenied: (, controller) { showPermissionNotGrantedDialog() .then(() => widget.backButtonCallback()); return false; }, onError: (, __) { _showQrCodeReadErrorDialog(); }, overlay: Container(), ); }
Future showQrCodeReadErrorDialog() { return showDialog( context: context, builder: () => AlertDialog( title: Text(_l10n.accountLinking_QrCodeScannerDialogTitle), content: Text(_l10n.accountLinking_QrCodeScannerDialogDescription), actions: [ TextButton( onPressed: () { Navigator.pop(context); }, child: Text(_l10n.accountLinking_QrCodeScannerDialogAction), ) ], ), ); }
Future showPermissionNotGrantedDialog() { return showDialog( context: context, builder: () => AlertDialog( title: Text( _l10n.accountLinking_QrCodeScannerMissingCameraPermissionTitle), content: Text(_l10n .accountLinking_QrCodeScannerMissingCameraPermissionDescription), actions: [ TextButton( onPressed: () { Navigator.pop(context); }, child: Text(_l10n.accountLinking_QrCodeScannerDialogAction), ) ], ), ); } }
- Assistant
Um diesen Fehler zu beheben, können Sie überprüfen, ob der
CameraControllerbereits entsorgt wurde, bevor Sie darauf aufrufen. Eine Möglichkeit, dies zu tun, besteht darin, eine Variable zum Speichern des Zustands des hinzuzufügen, z.B. . - User
Der fehlt tritt trotzdem noch auf. Er landet in der camera_controller.dart "throw CameraException" void _throwIfNotInitialized(String functionName) { if (!value.isInitialized) { throw CameraException( 'Uninitialized CameraController', 'functionName() was called on an uninitialized CameraController.', ); } if (_isDisposed) { throw CameraException( 'Disposed CameraController', 'functionName() was called on a disposed CameraController.', ); } }
- Assistant
Entschuldigung für das Missverständnis. Um dieses Problem zu lösen, können Sie versuchen, den Lebenszyklus der
_QrScannerState-Klasse zu verfolgen und den CameraController zum richtigen Zeitpunkt zu initialisieren und zu entsorgen. Sie können auch denStatefulWidgetLifecyclemixin zu Ihrer_QrScannerState-Klasse hinzufügen, um die Initialisierung und Entsorgung des CameraControllers in den initState- und dispose-Funktionen der State-Klasse zu behandeln.Hier ist ein Beispiel, wie Sie das tun können:
class _QrScannerState extends State<QrScanner> with WidgetsBindingObserver, StatefulWidgetLifecycle { ... @override void initState() { super.initState(); startCameraController(); } Future<void> startCameraController() async { if (!mounted) { return; } if (!_isCameraControllerInitialized) { // Initialize the camera controller here // ... _isCameraControllerInitialized = true; } } @override void dispose() { _isCameraControllerDisposed = true; // Update the state when resources are released stopCameraController(); super.dispose(); } Future<void> stopCameraController() async { if (_isCameraControllerInitialized) { // Dispose the camera controller here // ... _isCameraControllerInitialized = false; } } ... }