错误:无法将参数类型“List<int>”分配给参数类型“Uint8List”

时间:2021-06-14 15:30:30

标签: dart-null-safety uint8list

下面是我的原始代码在 flutter 1.22.6 版本下运行良好,没有问题,但是当我升级到 flutter 2.2.1 版本时出现“无法将参数类型‘List’分配给参数类型‘Uint8List’的错误.”标记为红色错误:

最终绘制 = 等待 PaintBinding.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;

}

...

1 个答案:

答案 0 :(得分:0)

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

Uint8List uint8List;
List<int> list;  

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

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

干杯!

大卫