Flutter Barcode_scan plugin crashing App?

1,906

You're probably not adding the dependency in your AndroidManifest.xml folder, The same issue I had, Just modify scanQR() function.

Try this:

Future scanQR() async {
    try {
      String barcode;
      await BarcodeScanner.scan().then((onValue) {
        setState(() {
          barcode = onValue.toString();
        });
      }).catchError((onError) {
        print(onError);
      });
      setState(() => this.barcode = barcode);
    } on PlatformException catch (e) {
      if (e.code == BarcodeScanner.CameraAccessDenied) {
        setState(() {
          this.barcode = 'camera permission not granted!';
        });
      } else {
        setState(() => this.barcode = 'Unknown error: $e');
      }
    } on FormatException {
      setState(() => this.barcode = '(User returned)');
    } catch (e) {
      setState(() => this.barcode = 'Unknown error: $e');
    }
}
Share:
1,906
Harshit Vadhera
Author by

Harshit Vadhera

Updated on December 10, 2022

Comments

  • Harshit Vadhera
    Harshit Vadhera over 1 year

    As Soon As I Click on The Camera Button The App Crashes.

    It is not Asking For Any Permission Also.

    I Have Included The Activity File in the Manifest Also

    This is The Code I Am Using and The plugin I Am Using is barcode_scan:

    Even After Searching a Lot I Am Unable To get to The Problem. Help Would Be Appreciated.

    class _AuditPage extends State<AuditPage>{
     String result = "Scan The Barcode!";
    
    
     Future _scanQR() async{
        try{
          String qrCode = await BarcodeScanner.scan();
          setState(() {
            result = qrCode;
          });
        }on PlatformException catch (ex){
          if(ex.code==BarcodeScanner.CameraAccessDenied){
            setState(() {
              result="Camera Permission Was Denied";
            });
          }else{
            setState(() {
              result="Unknown Error $ex!";
            });
          }
        } on FormatException {
          setState(() {
            result = "You Pressed The Back Button Before Scanning";
          });
        } catch (ex){
          setState(() {
              result="Unknown Error $ex!";
            });
        }
     }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
           resizeToAvoidBottomPadding: false,
          appBar: AppBar(
            title: Text("Bhaifi"),
          ),
          drawer: DrawerPage(),
          body: Center(
            child:Text(
              result,
            ),
          ), 
            floatingActionButton:FloatingActionButton.extended(
              icon: Icon(Icons.camera_alt),
              label: Text("Scan"),
              onPressed: _scanQR,
            ),
          floatingActionButtonLocation:FloatingActionButtonLocation.centerFloat,
        );
      }
    
    
    }
    
    • Francesco Iapicca
      Francesco Iapicca about 5 years
      you want to take a look to AndroidX issue: I had a similar situation, you can grab some code from this post: stackoverflow.com/questions/55223002/… not the solution or the updates, but my troubleshooting in the original post; I hope it helps
    • Harshit Vadhera
      Harshit Vadhera about 5 years
      Thanks But Not Working
    • Francesco Iapicca
      Francesco Iapicca about 5 years
      try using the "old one": barcode_scan: ^0.0.8 , if doesn't work try without " ^ ": just "barcode_scan: 0.0.8"
    • Viren V Varasadiya
      Viren V Varasadiya about 5 years
      do not specify the version of barcode_scan it will tack suitable version for you.