Harshit Vadhera
Harshit Vadhera

Reputation: 160

Flutter Barcode_scan plugin crashing App?

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


}

Upvotes: 1

Views: 2411

Answers (1)

Ayyaz Shair
Ayyaz Shair

Reputation: 620

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');
    }
}

Upvotes: 1

Related Questions