optimiere den Code. Außerdem behebe das Fehler. Es wird versucht den Kameracontroller zu verwerfen, obwohl er schon verworfen wurde. 
Wie und wo behebe ich das? Erkläre es ausführlich und lass nichts weg. 

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),
          )
        ],
      ),
    );
  }
}

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/modules/account_linking/account_linking_state_manager.dart';
import 'package:patient_app/modules/account_linking/widgets/qr_scanner.dart'
    if (dart.library.html) 'package:patient_app/modules/account_linking/widgets/qr_scanner_mock.dart';
import 'package:patient_app/modules/account_linking/widgets/qr_scanner_unmaintained.dart'
    as qr_code_scanner_unmaintained;
import 'package:patient_app/settings.dart' as settings;
import 'package:patient_app/theme/patient_app_theme.dart' as theme;
import 'package:provider/provider.dart';

class AccountLinkingQrCodeScannerScreen extends StatefulWidget {
  static MaterialPage page() {
    return const MaterialPage(
      name: "QrCodeScanner",
      key: ValueKey("QrCodeScanner"),
      child: AccountLinkingQrCodeScannerScreen(),
    );
  }

  const AccountLinkingQrCodeScannerScreen({Key? key}) : super(key: key);

  @override
  State<AccountLinkingQrCodeScannerScreen> createState() =>
      _AccountLinkingQrCodeScannerScreenState();
}

class _AccountLinkingQrCodeScannerScreenState
    extends State<AccountLinkingQrCodeScannerScreen> {
  late AppLocalizations _l10n;

  @override
  void didChangeDependencies() {
    _l10n = AppLocalizations.of(context)!;
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        alignment: Alignment.center,
        fit: StackFit.expand,
        children: <Widget>[
          _buildQrScannerArea(),
          _buildMainContent(),
        ],
      ),
    );
  }

  Widget _buildMainContent() {
    return Padding(
      padding: theme.commonEdgeInsets,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          const SizedBox(
            height: theme.extraLargeSpacing,
          ),
          Text(
            _l10n.accountLinking_QrCodeScannerScreenTitle,
            style: theme.lightHeadline1,
          ),
          const SizedBox(height: theme.extraSmallSpacing),
          Text(
            _l10n.accountLinking_QrCodeScannerScreenDescription,
            style: theme.lightBodyText2,
          ),
          const Spacer(),
          Center(
            child: ElevatedButton(
              onPressed: () {
                context.read<AccountLinkingStateManager>().deleteState();
              },
              style: Theme.of(context).elevatedButtonTheme.style,
              child: Text(_l10n.general_cancelButtonLabel),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildQrScannerArea() {
    Widget child = settings.useUnmaintainedQrScanner
        ? qr_code_scanner_unmaintained.QrScanner(
            callback: _handleResult, backButtonCallback: _backButtonCallback)
        : QrScanner(
            callback: _handleResult,
            backButtonCallback: _backButtonCallback,
          );
    return Positioned.fill(
      child: child,
    );
  }

  Future<bool> _handleResult(ScanResult scanResult) async {
    if (isValid(scanResult)) {
      context.read<AccountLinkingStateManager>().updateScanResult(scanResult);
      return true;
    } else {
      return false;
    }
  }

  // prevent qr code fragments to be accepted
  // Large qr codes could result in partially identified qr codes,
  // they are too short to be accepted
  bool isValid(ScanResult scanResult) => scanResult.qrCodeAsString.length > 16;

  void _backButtonCallback() {
    context.read<AccountLinkingStateManager>().deleteState();
  }
}