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

时间:2019-03-25 08:26:04

标签: android dart flutter barcode

当我单击“相机”按钮时,应用程序崩溃。

它也不要求任何权限。

我也将活动文件包括在清单中

这是我正在使用的代码,而我正在使用的插件是barcode_scan:

即使搜索了很多,我也无法解决问题。 帮助将不胜感激。

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


}

1 个答案:

答案 0 :(得分:0)

您可能没有在您的AndroidManifest.xml文件夹中添加依赖项,同样的问题,只需修改scanQR()函数即可。

尝试一下:

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