StateError (Bad state: No element) on IOS only

Viewed 109

This error does not occur on Android or web but only on IOS. It seem very trivial but I can't figure out what's wrong.

import 'dart:developer';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'dart:convert';

import 'package:qr_code_scanner/qr_code_scanner.dart';

class ScanQrPage extends StatefulWidget {
  @override
  _ScanQrPageState createState() => _ScanQrPageState();
}

class _ScanQrPageState extends State<ScanQrPage> {
  final qrKey = GlobalKey();

  late QRViewController qrViewController;

  late Barcode barcode;

  // In order to get hot reload to work we need to pause the camera if the platform
  // is android, or resume the camera if the platform is iOS.
  @override
  void reassemble() {
    super.reassemble();
    if (Platform.isAndroid) {
      qrViewController.pauseCamera();
    } else if (Platform.isIOS) {
      qrViewController.resumeCamera();
    }
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () {
        Navigator.of(context).pop("");
        return new Future(() => true);
      },
      child: Scaffold(
        body: Stack(
          children: [
            buildQrView(context),
          ],
        ),
      ),
    );
  }

  Widget buildQrView(BuildContext context) {
    return QRView(
      onQRViewCreated: onQRViewCreated,
      key: qrKey,
      overlay: QrScannerOverlayShape(
          cutOutSize: MediaQuery.of(context).size.width * 0.8),
      onPermissionSet: (ctrl, p) => _onPermissionSet(context, ctrl, p),
    );
  }

  void _onPermissionSet(BuildContext context, QRViewController ctrl, bool p) {
    log('${DateTime.now().toIso8601String()}_onPermissionSet $p');
    if (!p) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('no Permission')),
      );
    }
  }

  @override
  void dispose() {
    qrViewController.dispose();
    super.dispose();
  }

  void onQRViewCreated(QRViewController qrViewController) {
    setState(() {
      this.qrViewController = qrViewController;
    });

    qrViewController.scannedDataStream.listen((event) {
      setState(() {
        this.barcode = event;
        if (Platform.isAndroid) {
          qrViewController.pauseCamera();
        } else if (Platform.isIOS) {
          qrViewController.resumeCamera();
        }
        String rawData = event.code;
        Uri data = Uri.dataFromString(rawData);
        String para1 = data.queryParameters["buy"] ??
            ""; //get parameter with attribute "para1"
        

        Codec<String, String> stringToBase64 = utf8.fuse(base64);
        if (para1 != "") {
          placer = stringToBase64.decode(para1);

        }

        WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
          Navigator.pop(context, placer);
        });

        
      });
    });
  }
}

I've tried all the solutions with the same error found on stackoverflow (addPostFrameCallback and Future(Duration.zero)) but none of them are exactly the same and does not seem to fix my problem.

I don't think I have having the same issue as any of the other questions.

The exception is happening on the Navigator.pop(context, placer);

Does anyone have any idea how to overcome this?

Why does this only happen on IOS?

0 Answers
Related