简体   繁体   English

Flutter:未处理的异常:类型“_ByteDataView”不是“Uint8List”类型的子类型

[英]Flutter: Unhandled Exception: type '_ByteDataView' is not a subtype of type 'Uint8List'

I am trying to create a custom marker with Round Image File我正在尝试使用 圆形图像文件创建自定义标记

However, I am running into the [VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: type '_ByteDataView' is not a subtype of type 'Uint8List' error.但是,我遇到了[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: type '_ByteDataView' is not a subtype of type 'Uint8List'错误。

  resizeImage(Uint8List file,
      {int size = 150,
      bool addBorder = true,
      Color borderColor = Colors.orange,
      double borderSize = 20,
      Color titleColor = Colors.white,
      Color titleBackgroundColor = Colors.black}) async {
    final ui.PictureRecorder pictureRecorder = ui.PictureRecorder();
    final Canvas canvas = Canvas(pictureRecorder);
    final Paint paint = Paint()..color;
    final double radius = size / 2;

    //make canvas clip path to prevent image drawing over the circle
    final Path clipPath = Path();
    clipPath.addRRect(RRect.fromRectAndRadius(
        Rect.fromLTWH(0, 0, size.toDouble(), size.toDouble()),
        Radius.circular(100)));
    canvas.clipPath(clipPath);

    //paintImage

    ui.Codec codec = await ui.instantiateImageCodec(file);
    ui.FrameInfo frame = await codec.getNextFrame();

    paintImage(
        canvas: canvas,
        rect: Rect.fromLTWH(0, 0, size.toDouble(), size.toDouble()),
        image: frame.image);

    if (addBorder) {
      //draw Border
      paint..color = borderColor;
      paint..style = PaintingStyle.stroke;
      paint..strokeWidth = borderSize;
      canvas.drawCircle(Offset(radius, radius), radius, paint);
    }

    //convert canvas as PNG bytes
    final _image = await pictureRecorder
        .endRecording()
        .toImage(size, (size * 1.1).toInt());
    final data = await _image.toByteData(format: ui.ImageByteFormat.png);

    //convert PNG bytes as BitmapDescriptor
    return data;
  }

I'd tried with the asset images, network image and more.我尝试过使用资产图像、网络图像等。 All have the errors.都有错误。 I've checked with other people's questions.我已经检查过其他人的问题。 However, I have no clue what are the differences and suddenly I am getting these errors?但是,我不知道有什么区别,突然我收到这些错误?

What have I done wrong here and how can make it work?我在这里做错了什么,如何让它发挥作用?

Your function returns Future<ByteData?> so let the compiler help you by declaring that as the return type.您的 function 返回Future<ByteData?>所以让编译器通过将其声明为返回类型来帮助您。 My guess is that where you use the function you are expecting the result to be Uint8List .我的猜测是,在您使用 function 的地方,您期望结果是Uint8List If you change that, the compiler should help you find it.如果您更改它,编译器应该会帮助您找到它。

Here's an updated and tidied version (with clearer cascades, removal of obvious types etc)这是一个更新和整理的版本(具有更清晰的级联,删除了明显的类型等)

Future<ByteData?> resizeImage(
  Uint8List file, {
  int size = 150,
  bool addBorder = true,
  Color borderColor = Colors.orange,
  double borderSize = 20,
  Color titleColor = Colors.white,
  Color titleBackgroundColor = Colors.black,
}) async {
  final pictureRecorder = ui.PictureRecorder();
  final canvas = Canvas(pictureRecorder);

  //make canvas clip path to prevent image drawing over the circle
  canvas.clipPath(
    Path()
      ..addRRect(RRect.fromRectAndRadius(
        Rect.fromLTWH(0, 0, size.toDouble(), size.toDouble()),
        const Radius.circular(100),
      )),
  );

  //paintImage
  final codec = await ui.instantiateImageCodec(file);
  final frame = await codec.getNextFrame();

  paintImage(
    canvas: canvas,
    rect: Rect.fromLTWH(0, 0, size.toDouble(), size.toDouble()),
    image: frame.image,
  );

  if (addBorder) {
    //draw Border
    final radius = size / 2;

    final paint = Paint()
      ..color = borderColor
      ..style = PaintingStyle.stroke
      ..strokeWidth = borderSize;
    canvas.drawCircle(Offset(radius, radius), radius, paint);
  }

  //convert canvas as PNG bytes
  final _image = await pictureRecorder.endRecording().toImage(
        size,
        (size * 1.1).toInt(),
      );

  return await _image.toByteData(format: ui.ImageByteFormat.png);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未处理的异常:“Uint8List”类型不是“int”类型的子类型 - Unhandled Exception: type 'Uint8List' is not a subtype of type 'int' 颤振:输入“未来”<dynamic> &#39; 不是类型 &#39;Uint8List&#39; 的子类型 - Flutter: type 'Future<dynamic>' is not a subtype of type 'Uint8List' 错误类型'列表<dynamic> ' 不是类型 'List 的子类型<uint8list> ? 在 Flutter</uint8list></dynamic> - Error type 'List<dynamic>' is not a subtype of type 'List<Uint8List>?' in Flutter 输入&#39;列表<dynamic> &#39; 不是类型 &#39;Uint8List 的子类型 - type 'List<dynamic>' is not a subtype of type 'Uint8List 输入“未来<uint8list> ' 不是类型 'Widget' 的子类型</uint8list> - type 'Future<Uint8List>' is not a subtype of type 'Widget' ''列表<object?> ' 不是 IOS Flutter 平台频道上的 'Uint8List?' 类型的子类型</object?> - ''List<Object?>' is not a subtype of type 'Uint8List?'' on IOS Flutter Platform Channel Flutter '未处理的异常:类型'列表<dynamic> ' 不是类型 'List 的子类型<class> '</class></dynamic> - Flutter 'Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<class>' Flutter:未处理的异常:键入“列表”<dynamic> ' 不是类型 'List 的子类型<string> '</string></dynamic> - Flutter:Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<String>' Dart 未处理的异常:键入“列表”<dynamic> ' 不是类型 'Family' 的子类型,- Flutter</dynamic> - Dart Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Family', - Flutter 未处理的异常:键入“列表”<dynamic> ' 不是 'SubJsonModel' 类型的子类型 - flutter</dynamic> - Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'SubJsonModel' - flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM