“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<bool> 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<QrScanner> createState() => _QrScannerState();
}

class _QrScannerState extends State<QrScanner> 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<dynamic> _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<dynamic> _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),
          )
        ],
      ),
    );
  }
}
