Flutter 参数类型'List<int>'无法赋值给参数类型'Uint8List'

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

我有一个代码(不是我最初写的),在尝试更新它时,我收到了这个错误:

ui.encodePng(temp)  "The argument type 'List<int>' can't be assigned to the parameter type 'Uint8List'"

这是基本代码:

    // create crop image for each block
    ui.Image temp = ui.copyCrop(
      fullImage,
      xAxis.round(),
      yAxis.round(),
      widthPerBlockTemp.round(),
      heightPerBlockTemp.round(),
    );

    // get offset for each block show on center base later
    Offset offset = Offset(size.width / 2 - widthPerBlockTemp / 2,
        size.height / 2 - heightPerBlockTemp / 2);

    ImageBox imageBox = new ImageBox(
      image: Image.memory(
        ui.encodePng(temp),
        fit: BoxFit.contain,
      ),
      isDone: false,
      offsetCenter: offsetCenter,
      posSide: jigsawPosSide,
      radiusPoint: minSize,
      size: Size(widthPerBlockTemp, heightPerBlockTemp),
    );

    images[y].add(
      new BlockClass(
          jigsawBlockWidget: JigsawBlockWidget(
            imageBox: imageBox,
          ),
          offset: offset,
          offsetDefault: Offset(xAxis, yAxis)),
    );

编辑:好的,这是一个完整的代码

  Future<void> generaJigsawCropImage() async {
// 1st we need create a class for block image
// ignore: deprecated_member_use
images = <List<BlockClass>>[];

// get image from out boundary

if (fullImage == null) fullImage = await _getImageFromWidget();

// split image using crop

int xSplitCount = 2;
int ySplitCount = 2;

// i think i know what the problom width & height not correct!
double widthPerBlock =
    fullImage.width / xSplitCount; // change back to width
double heightPerBlock = fullImage.height / ySplitCount;

for (var y = 0; y < ySplitCount; y++) {
  // temporary images
  // ignore: deprecated_member_use
  //List tempImages = List<BlockClass>[];

  images.add(<BlockClass>[]);
  for (var x = 0; x < xSplitCount; x++) {
    int randomPosRow = math.Random().nextInt(2) % 2 == 0 ? 1 : -1;
    int randomPosCol = math.Random().nextInt(2) % 2 == 0 ? 1 : -1;

    Offset offsetCenter = Offset(widthPerBlock / 2, heightPerBlock / 2);

    // make random jigsaw pointer in or out

    ClassJigsawPos jigsawPosSide = new ClassJigsawPos(
      bottom: y == ySplitCount - 1 ? 0 : randomPosCol,
      left: x == 0
          ? 0
          : -images[y][x - 1]
              .jigsawBlockWidget
              .imageBox
              .posSide
              .right, // ops.. forgot to dclare
      right: x == xSplitCount - 1 ? 0 : randomPosRow,
      top: y == 0
          ? 0
          : -images[y - 1][x].jigsawBlockWidget.imageBox.posSide.bottom,
    );

    double xAxis = widthPerBlock * x;
    double yAxis = heightPerBlock * y; // this is culprit.. haha

    // size for pointing
    double minSize = math.min(widthPerBlock, heightPerBlock) / 15 * 4;

    offsetCenter = Offset(
      (widthPerBlock / 2) + (jigsawPosSide.left == 1 ? minSize : 0),
      (heightPerBlock / 2) + (jigsawPosSide.top == 1 ? minSize : 0),
    );

    // change axis for posSideEffect
    xAxis -= jigsawPosSide.left == 1 ? minSize : 0;
    yAxis -= jigsawPosSide.top == 1 ? minSize : 0;

    // get width & height after change Axis Side Effect
    double widthPerBlockTemp = widthPerBlock +
        (jigsawPosSide.left == 1 ? minSize : 0) +
        (jigsawPosSide.right == 1 ? minSize : 0);
    double heightPerBlockTemp = heightPerBlock +
        (jigsawPosSide.top == 1 ? minSize : 0) +
        (jigsawPosSide.bottom == 1 ? minSize : 0);

    // create crop image for each block
    ui.Image temp = ui.copyCrop(
      fullImage,
      xAxis.round(),
      yAxis.round(),
      widthPerBlockTemp.round(),
      heightPerBlockTemp.round(),
    );

    // get offset for each block show on center base later
    Offset offset = Offset(size.width / 2 - widthPerBlockTemp / 2,
        size.height / 2 - heightPerBlockTemp / 2);

    ImageBox imageBox = new ImageBox(
      image: Image.memory(
        ui.encodePng(temp),
        fit: BoxFit.contain,
      ),
      isDone: false,
      offsetCenter: offsetCenter,
      posSide: jigsawPosSide,
      radiusPoint: minSize,
      size: Size(widthPerBlockTemp, heightPerBlockTemp),
    );

    images[y].add(
      new BlockClass(
          jigsawBlockWidget: JigsawBlockWidget(
            imageBox: imageBox,
          ),
          offset: offset,
          offsetDefault: Offset(xAxis, yAxis)),
    );
  }
}

blocksNotifier.value = images.expand((image) => image).toList();
// let random a bit so blok puzzle not in incremet order
// ops..bug .. i found culprit.. seem RepaintBoundary return wrong width on render..fix 1st using height
// as well
blocksNotifier.value.shuffle();
blocksNotifier.notifyListeners();
// _index = 0;
setState(() {});
}

我是新来的,所以直到现在我都不知道如何编辑,

您可以使用 Uint8List.fromListList<int> 构建 Uint8List

您也可以尝试使用显式转换(例如 list as Uint8List),因为很多代码声明为 return List<int> 但实际上 returns Uint8List 对象。 (在您的情况下,假设您使用的是 package:image,在浏览完文档后,encodePng 确实是这种情况。)使用强制转换可以避免创建不必要的副本(这可能是可能很贵)。