简体   繁体   English

Flutter Barcode_scan 插件使应用程序崩溃?

[英]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:这是我使用的代码,我使用的插件是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,
    );
  }


}

You're probably not adding the dependency in your AndroidManifest.xml folder, The same issue I had, Just modify scanQR() function.您可能没有在AndroidManifest.xml文件夹中添加依赖项,我遇到了同样的问题,只需修改scanQR()函数即可。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM