You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter QR扫码返回Instance of 'Barcode' 如何转为字符串正常展示

问题原因

你调用toString()的对象是Barcode类的实例,该类默认的toString方法只会返回实例类型描述,扫码得到的实际内容存储在Barcode实例的code属性中。

修改方案

任选以下一种方式修改即可:

方案1:扫码回调直接返回字符串结果

修改QR扫码组件的实现逻辑,页面返回时直接传递扫码得到的字符串内容:

QRView(
  key: _qrKey,
  overlay: QrScannerOverlayShape(borderColor: Colors.white),
  onQRViewCreated: (QRViewController controller) {
    this._controller = controller;
    controller.scannedDataStream.listen((val) {
      if (mounted) {
        _controller.dispose();
        // 直接返回code属性的字符串内容
        Navigator.pop(context, val.code);
      }
    });
  }),

方案2:接收返回结果时提取字符串

修改打开扫码页的逻辑,拿到Barcode实例后提取code属性,同时增加空值处理避免异常:

Future _openScanner(BuildContext context) async {
  final result = await Navigator.push(context, MaterialPageRoute(builder: (c) => Scanner()));
  // 提取code属性,空值时返回默认提示
  _result = result?.code ?? '未识别到有效内容';
}

补充说明

如果你的扫码工具需要支持多种条码格式,还可以通过Barcode实例的format属性判断扫码类型,做针对性的业务逻辑处理。

内容的提问来源于stack exchange,提问作者Caramel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 13:09:04