Flutter 参数类型“List”无法分配给参数类型“Uint8List”

2023-12-07

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

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.fromList构建一个Uint8List from a List<int>.

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

我通常会结合这些方法。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Flutter 参数类型“List”无法分配给参数类型“Uint8List” 的相关文章

随机推荐