Home > front end >  Flutter - Close barcode scanner after scan in flutter
Flutter - Close barcode scanner after scan in flutter

Time:12-06

I am using this package package for scanning barcode. It works fine but after successfully scan I want to close the camera. I searched for the solution but no solution worked in my case. Here is my code.

    try {
      FlutterBarcodeScanner.getBarcodeStreamReceiver(
              '#ff6666', 'Cancel', true, ScanMode.BARCODE)!
          .listen((data) {
        print(data);
       
      });
    } on PlatformException {
      // barcodeScanRes = 'Failed to get platform version.';
    }
  }

CodePudding user response:

you can close scan page by adding this line to your code

if (barcodeScanRes != null){
   print(barcodeScanRes);
   // this will send your scan result to previous page
   // or you can navigate to other page after scan success
   Navigator.pop(context, barcodeScanRes); 
}
String barcodeScanRes; // put this variable in statefull widget
Future<void> scanQR() async {
  try {
    barcodeScanRes = await FlutterBarcodeScanner.scanBarcode(
        '#ff6666', 'Cancel', true, ScanMode.QR);
    // add this line to close scanner or naivigate to other page
    if (barcodeScanRes != null){
       print(barcodeScanRes);
       Navigator.pop(context, barcodeScanRes);
    }
  } on PlatformException {
    barcodeScanRes = 'Failed to get platform version.';
  }

  if (!mounted) return;
  setState(() {
    _scanBarcode = barcodeScanRes;
  });
}

or if you want to pause the camera after scan, you can use this package https://pub.dev/packages/qr_code_scanner

  • Related