在Flutter中尝试截图,但遇到了异常。访问了许多链接,但没有任何作用。
package:flutter/src/rendering/proxy_box.dart':失败的断言:第2813行第12个字符:'!debugNeedsPaint':不为真。
Future<Uint8List> _capturePng() async {
try {
print('inside');
RenderRepaintBoundary boundary = _globalKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage(pixelRatio: 3.0);
ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
var pngBytes = byteData.buffer.asUint8List();
var bs64 = base64Encode(pngBytes);
print(pngBytes);
print(bs64);
setState(() {});
return pngBytes;
} catch (e) {
print(e);
}
}
StatefulWidget
的initState
中被调用,很可能不起作用,因为从_globalKey
上下文中检索到的RenderObject
尚未被绘制/绘画。 - Ovidiu