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

Time:02-18

I have a code (not originally written by me) and, while trying to update it, I'm getting this error:

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

This is the base code:

    // 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)),
    );

EDIT: okey, this is a code complete

  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(() {});
}

I'm new here so I didn't know how to edit until now,

CodePudding user response:

You can use Uint8List.fromList to construct a Uint8List from a List<int>.

You alternatively could try using an explicit cast (e.g. list as Uint8List) since a lot of code is declared to return List<int> but actually returns Uint8List objects.(Assuming that you're using package:image and skimming through the documentation, encodePng does appear to be one such case.) Using a cast would avoid creating an unnecessary copy (which could be potentially large).

I normally combine the approaches:

/// Converts a `List<int>` to a [Uint8List].
///
/// Attempts to cast to a [Uint8List] first to avoid creating an unnecessary
/// copy.
extension ToUint8List on List<int> {
  Uint8List toUint8List() {
    // Only local variables can be type-promoted automatically.
    final self = this;
    return (self is Uint8List) ? self : Uint8List.fromList(this);
  }
}

and then use it as someList.toUint8List().

CodePudding user response:

My mistake, I didn't know how to edit it until now, for the same reason I won't delete it to be able to know about my mistakes and not hide them

this is the complete code of the function

  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(() {});
}
  • Related