How to fix this error message:
The method ‘toImage’ isn’t defined for the type ‘RenderObject’. Try
correcting the name to the name of an existing method, or defining a
method named ‘toImage’.
My Code:
Future<void> saveImage() async {
RenderObject? boundary = globalKey.currentContext!.findRenderObject() as RenderBox;
ui.Image image = await boundary.toImage();
ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List pngBytes = byteData!.buffer.asUint8List();
//Request permissions if not already granted
if (!(await Permission.storage.status.isGranted))
await Permission.storage.request();
final result = await ImageGallerySaver.saveImage(
Uint8List.fromList(pngBytes),
quality: 60,
name: "logo_image");
print(result);
}
2
Answers
You will have to wrap the widget you want to convert to image into
RepaintBoundary
widget withglobalKey
.Once that is done, you can change
to
That is because the RenderObject class (or even RenderBox) do not define
toImage
method. Instead you will have to cast to RenderRepaintBoundary.You are writing
but in the docu they write
try correcting that…