Error: The argument type 'List<int>' can't be assigned to the parameter type 'Uint8List'

Viewed 2578

Below is my original codes run well without problem under flutter version 1.22.6, however when I upgraded to flutter version 2.2.1 error of "The argument type 'List' can't be assigned to the parameter type 'Uint8List'." flagged with red error:

final paint = await PaintingBinding.instance! .instantiateImageCodec(asset != null ? img.encodePng(asset) : buffer);

Anyone could help will be much appreaciated.

This my code:

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 Answers

This is because they are different types. But here is an easy way to handle the conversion:

Uint8List uint8List;
List<int> list;  

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

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

Cheers!

David

Related