【问题标题】:Flutter - [barcode_scan] ScanResult is not a subtype of type StringFlutter - [barcode_scan] ScanResult 不是 String 类型的子类型
【发布时间】:2020-12-09 19:21:31
【问题描述】:

我尝试使用带有以下代码的 Flutter 扫描带有 barcode_scan 的二维码,但我一直收到错误: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,
      ),
    );
    }
  }

【问题讨论】:

    标签: flutter barcode-scanner


    【解决方案1】:

    您可以使用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,
          ),
        );
      }
    }
    

    【讨论】:

    • 非常感谢 Stefano。它工作得很好:)
    • 乐于助人!不要忘记选择正确的答案:)
    猜你喜欢
    • 2021-10-06
    • 1970-01-01
    • 2023-01-26
    • 2021-07-18
    • 2021-07-10
    • 2020-08-01
    • 2020-10-19
    • 2021-07-02
    • 2019-10-15
    相关资源
    最近更新 更多