Flutter - [barcode_scan] ScanResult 不是 String 类型的子类型

Flutter - [barcode_scan] ScanResult is not a subtype of type String

我尝试使用 Flutter 和下面的代码通过 barcode_scan 扫描 QR 码,但我一直收到错误消息:unknown error type 'ScanResult' is not a subtype of type 'String' in typecast Scan.

有人可以给我解释一下吗?或者有人对此有想法吗?

 import 'package:barcode_scan/gen/protos/protos.pbserver.dart';
    import 'dart:async';
    import 'package:flutter/material.dart';
    import 'package:barcode_scan/barcode_scan.dart';
    import 'package:flutter/services.dart';
    import 'package:qr_flutter/qr_flutter.dart';


    class Scanner extends StatefulWidget {
    @override
    _ScannerState createState() => _ScannerState();
    }  

    class _ScannerState extends State<Scanner> {
    String result = "press the camera to start the scan !";

    Future _scanQR() async {
    try {
      String qrResult = await BarcodeScanner.scan() as String;
      setState(() {
        result = qrResult;
      });
    } on PlatformException catch (ex) {
      if (ex.code == BarcodeScanner.cameraAccessDenied) {
        setState(() {
          result = "Camera was denied";
        });
      } else {
        setState(() {
          result = "Unknown Error $ex";
        });
      }
    } on FormatException {
      setState(() {
        result = "You pressed the back button before scanning anything";
      });
    } catch (ex) {
      setState(() {
        result = "Unknown Error $ex";
      });
    }
    }

    @override
    Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("QR Scanner"),
      ),
      body: Center(
        child: Text(
          result,
          style: new TextStyle(fontSize: 30.0, fontWeight: FontWeight.bold),
        ),
      ),
      floatingActionButton: FloatingActionButton.extended(
        icon: Icon(Icons.camera_alt),
        label: Text("Scan"),
        onPressed: _scanQR,
      ),
    );
    }
  }

您可以使用 rawContent 属性 作为 String:

访问数据
ScanResult qrScanResult = await BarcodeScanner.scan();
String qrResult = qrScanResult.rawContent;

遵循完整示例:

import 'package:barcode_scan/gen/protos/protos.pbserver.dart';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:barcode_scan/barcode_scan.dart';
import 'package:flutter/services.dart';
import 'package:qr_flutter/qr_flutter.dart';

class Scanner extends StatefulWidget {
  @override
  _ScannerState createState() => _ScannerState();
}

class _ScannerState extends State<Scanner> {
  String result = "press the camera to start the scan !";

  Future _scanQR() async {
    try {
      ScanResult qrScanResult = await BarcodeScanner.scan();
      String qrResult = qrScanResult.rawContent;
      setState(() {
        result = qrResult;
      });
    } on PlatformException catch (ex) {
      if (ex.code == BarcodeScanner.cameraAccessDenied) {
        setState(() {
          result = "Camera was denied";
        });
      } else {
        setState(() {
          result = "Unknown Error $ex";
        });
      }
    } on FormatException {
      setState(() {
        result = "You pressed the back button before scanning anything";
      });
    } catch (ex) {
      setState(() {
        result = "Unknown Error $ex";
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("QR Scanner"),
      ),
      body: Center(
        child: Text(
          result,
          style: new TextStyle(fontSize: 30.0, fontWeight: FontWeight.bold),
        ),
      ),
      floatingActionButton: FloatingActionButton.extended(
        icon: Icon(Icons.camera_alt),
        label: Text("Scan"),
        onPressed: _scanQR,
      ),
    );
  }
}

嗯,最重要的事情发生在这里:

String qrResult = await BarcodeScanner.scan() as String;
  setState(() {
    result = qrResult;
  });

我们要改两点,类型String不好,改成BarcodeResult,如果报错,就把类型改成var。 最后,要获得扫描仪结果,只需输入 result = qrResult.rawContent;

最终结果在这里:

var qrResult = await BarcodeScanner.scan();
  setState(() {
    result = qrResult.rawContent;
  });