[英]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.