【问题标题】:Unable to display the contents of a barcode using barcode scanner无法使用条码扫描仪显示条码的内容
【发布时间】:2020-11-07 07:11:48
【问题描述】:

我一直在尝试使用条形码扫描仪显示商品的条形码... 我已经编写了用于扫描条形码的代码,但由于它引发错误而无法显示。 这段代码能够先打开相机然后扫描条形码,直到这里它工作得很好,但是当我试图显示条形码时它会抛出一个异常。

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io';
import 'package:barcode_scan/barcode_scan.dart';
import 'package:flutter/services.dart';


class CartItems extends StatefulWidget {
  @override
  _CartItemsState createState() => _CartItemsState();
}

class _CartItemsState extends State<CartItems> {

  String barcode = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(child: Text('Code: ' + barcode)),
      floatingActionButton: FloatingActionButton(
        onPressed: barcodeScanning,
        child: Icon(Icons.add),
      ),
    );
  }

  Future barcodeScanning() async {
    try {
      String barcode = BarcodeScanner.scan() as String;
      setState(() => this.barcode = barcode);
    } on PlatformException catch (e) {
      if (e.code == BarcodeScanner.cameraAccessDenied) {
        setState(() {
          this.barcode = 'No camera permission!';
        });
      } else {
        setState(() => this.barcode = 'Unknown error: $e');
      }
    } on FormatException {
      setState(() =>
      this.barcode =
      'Nothing captured.');
    } catch (e) {
      setState(() => this.barcode = 'Unknown error: $e');
    }
  }
}

请调查我正在努力解决错误的问题。

【问题讨论】:

  • 抛出哪个错误?还可以添加跟踪信息以更好地帮助我们回答您的问题。

标签: flutter dart barcode-scanner


【解决方案1】:

传递扫描仪函数返回的结果。通过这种方式,函数执行并将其发现提供给主上下文,还添加了一个 print() 以在底部查看结果,以查看控制台中可能出现的异常

class CartItems extends StatefulWidget {
  @override
  _CartItemsState createState() => _CartItemsState();
}

class _CartItemsState extends State<CartItems> {

  String barcodeResult = "";
  String barcode = "";


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(child: Text('Code: ' + barcode)),
      floatingActionButton: FloatingActionButton(
        onPressed:(){ barcodeScanning().then((value) 
       {
          setState(()
           {barcodeResult = value;})})}// this should work
        child: Icon(Icons.add),
      ),
    );
  }

  Future barcodeScanning() async {
    try {
      String barcode = BarcodeScanner.scan() as String;
      setState(() => this.barcode = barcode);
    } on PlatformException catch (e) {
      if (e.code == BarcodeScanner.cameraAccessDenied) {
        setState(() {
          this.barcode = 'No camera permission!';
        });
      } else {
        setState(() => this.barcode = 'Unknown error: $e');
      }
    } on FormatException {
      setState(() =>
      this.barcode =
      'Nothing captured.');
    } catch (e) {
      setState(() => this.barcode = 'Unknown error: $e');
    }
   return barcode;//this
  }
}

【讨论】:

  • onPressed:barcodeScanning.那么
  • .then 总是得到一条红色下划线,说明提取或创建“then”
  • 我已经修改过试试
  • 同样在定义全局变量后不要将其定义为其他地方的局部变量,在这种情况下您的字符串条形码
  • 请查看此错误。 stackoverflow.com/questions/63102887/…@king mort
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 2012-01-03
  • 2012-05-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多