Search code examples
flutterscreenshot

Error when trying to take screenshot with flutter RenderRepaintBoundary


RenderRepaintBoundary boundary = globalKey.currentContext.findRenderObject() 
                                  as RenderRepaintBoundary;

var image = await boundary.toImage(pixelRatio: pixelRatio);

boundary.toImage(pixelRatio: pixelRatio); ## throws error

[ERROR:flutter/shell/common/shell.cc(242)] Dart Unhandled Exception: NoSuchMethodError: The getter '_handles' was called on null.

Receiver: null
Tried calling: _handles, stack trace: #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
: #1      new Image._ (dart:ui/painting.dart:1601:12)
: #2      Scene.toImage.<anonymous closure>.<anonymous closure> (dart:ui/compositing.dart:35:24)

It was working fine two weeks ago but now it's throwing this error. How can I fix it?


Solution

  • So basically you have the problem is in boundary.toImage(pixelRatio: pixelRatio);

    change it to boundary.toImage();

    and it will work fine