【问题标题】:Error on printing our Images on Receipt via POS system通过 POS 系统在收据上打印我们的图像时出错
【发布时间】:2021-11-26 05:54:43
【问题描述】:

下面是我的代码。

 Future<List<int>> getImage() async {
    List<int> bytes = [];
    CapabilityProfile profile = await CapabilityProfile.load();
    final generator = Generator(PaperSize.mm80, profile);

    final ByteData data = await rootBundle.load('assets/logo.png');
    final buffer = data.buffer;
    final image = base64.encode(Uint8List.view(buffer));
    bytes += generator.image(image);

    return bytes;
  }

bytes += generator.image(image); 

错误提示

错误:不能将参数类型“字符串”分配给参数类型“图像”。

【问题讨论】:

  • base64 是一个字符串
  • @flaxon,知道我应该如何修复它吗?

标签: flutter pos


【解决方案1】:

来自The docs

你需要传递字节而不是 base64

您好像错过了decodeImage()

Future<List<int>> getImage() async {
    List<int> bytes = [];
    CapabilityProfile profile = await CapabilityProfile.load();
    final generator = Generator(PaperSize.mm80, profile);

    final ByteData data = await rootBundle.load('assets/logo.png');
    final Uint8List buffer = data.buffer.asUint8List();
    final Image image = decodeImage(buffer);

    bytes += generator.image(image);

    return bytes;
  }

【讨论】:

  • 最终的 Uint8List bytes= data.buffer.asUint8List(); , 错误: 'bytes' 已在此范围内声明。
  • 好的,所以使用另一个变量名。我改成buffer
  • 我错过了你的List&lt;int&gt; bytes = [];,但我认为它没有节点
  • 还有 1 个问题是最终的 Image 图像,名称 'Image' 定义在库 'package:flutter/src/widgets/image.dart' 和 'package:image/src/image.飞镖(通过包:image/image.dart)'。
  • 添加import 'package:image/image.dart'; on top
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-27
  • 1970-01-01
  • 2012-07-13
  • 1970-01-01
  • 1970-01-01
  • 2022-10-20
  • 1970-01-01
相关资源
最近更新 更多