【问题标题】:Error: The argument type 'List<int>' can't be assigned to the parameter type 'Uint8List'错误:无法将参数类型“List<int>”分配给参数类型“Uint8List”
【发布时间】:2021-08-30 12:44:48
【问题描述】:

下面是我的原始代码在flutter 1.22.6版下运行良好,但是当我升级到flutter 2.2.1版时出现“参数类型'List'不能分配给参数类型'Uint8List'的错误。”标记为红色错误:

最终绘制 = 等待 PaintingBinding.instance! .instantiateImageCodec(asset != null ? img.encodePng(asset) : buffer);

任何人都可以提供帮助将不胜感激。

这是我的代码:

Future<BeautifulPopup> recolor(Color color) async {
primaryColor = color;
final illustrationData = await rootBundle.load(instance.illustrationKey);
final buffer = illustrationData.buffer.asUint8List();
img.Image asset;
asset = img.readPng(buffer)!;

img.adjustColor(
  asset,
  saturation: 0,
  // hue: 0,
);
img.colorOffset(
  asset,
  red: primaryColor.red,
  // I don't know why the effect is nicer with the number ╮(╯▽╰)╭
  green: primaryColor.green ~/ 3,
  blue: primaryColor.blue ~/ 2,
  alpha: 0,
);

final paint = await PaintingBinding.instance!
    .instantiateImageCodec(asset != null ? img.encodePng(asset) : buffer);
final nextFrame = await paint.getNextFrame();
_illustration = nextFrame.image;
return this;

}

...

【问题讨论】:

    标签: dart-null-safety uint8list


    【解决方案1】:

    这是因为它们是不同的类型。但这里有一种处理转换的简单方法:

    Uint8List uint8List;
    List<int> list;  
    
    // convert List<int> to Uint8List. 
    uint8List = Uint8List.fromList(list);  
    
    // convert Uint8List to List<int>
    list = List<int>.from(uint8List);
    

    干杯!

    大卫

    【讨论】:

    • 为什么它们是不同的类型? "抽象类 Uint8List 实现 List, _TypedIntList"
    猜你喜欢
    • 2022-12-24
    • 2023-01-13
    • 1970-01-01
    • 2021-07-20
    • 2021-08-26
    • 2021-06-04
    • 1970-01-01
    • 2020-06-21
    • 2021-07-09
    相关资源
    最近更新 更多