@@ -55,46 +55,60 @@ class CatcherScreenshotManager {
55
55
56
56
Future <Uint8List ?> _capture ({
57
57
double ? pixelRatio,
58
- Duration ? delay = const Duration (milliseconds: 20 ),
58
+ Duration delay = const Duration (milliseconds: 20 ),
59
59
}) {
60
- return Future .delayed (delay ?? const Duration (milliseconds: 20 ), () async {
61
- final image = await _captureAsUiImage (
62
- delay: Duration .zero,
63
- pixelRatio: pixelRatio,
64
- );
65
- final byteData = await image.toByteData (format: ui.ImageByteFormat .png);
66
- final pngBytes = byteData? .buffer.asUint8List ();
60
+ //Delay is required. See Issue https://github.com/flutter/flutter/issues/22308
61
+ return Future .delayed (delay, () async {
62
+ try {
63
+ final image = await captureAsUiImage (
64
+ delay: Duration .zero,
65
+ pixelRatio: pixelRatio,
66
+ );
67
+ final byteData =
68
+ await image? .toByteData (format: ui.ImageByteFormat .png);
69
+ image? .dispose ();
70
+
71
+ final pngBytes = byteData? .buffer.asUint8List ();
67
72
68
- return pngBytes;
73
+ return pngBytes;
74
+ } catch (exception) {
75
+ _logger.severe ('Failed to capture screenshot: $exception ' );
76
+ }
77
+ return null ;
69
78
});
70
79
}
71
80
72
- Future <ui.Image > _captureAsUiImage ({
73
- double ? pixelRatio,
74
- Duration delay = const Duration (milliseconds: 100 ),
81
+ Future <ui.Image ?> captureAsUiImage ({
82
+ double ? pixelRatio = 1 ,
83
+ Duration delay = const Duration (milliseconds: 20 ),
75
84
}) {
85
+ //Delay is required. See Issue https://github.com/flutter/flutter/issues/22308
76
86
return Future .delayed (delay, () async {
77
- // ignore: cast_nullable_to_non_nullable
78
-
79
- final renderObject = _containerKey.currentContext? .findRenderObject ();
87
+ try {
88
+ final findRenderObject =
89
+ _containerKey.currentContext? .findRenderObject ();
80
90
81
- // ignore: unnecessary_null_comparison
82
- if (renderObject == null ) {
83
- throw StateError ('No boundary found' );
84
- }
85
-
86
- final boundary = renderObject as RenderRepaintBoundary ;
91
+ print (containerKey.currentContext);
92
+ print ( _containerKey.currentContext? .findRenderObject ());
93
+ if (findRenderObject == null ) {
94
+ return null ;
95
+ }
87
96
88
- final context = _containerKey.currentContext;
89
- var pixelRatioValue = pixelRatio;
90
- if (pixelRatioValue == null ) {
91
- if (context != null ) {
92
- pixelRatioValue =
93
- pixelRatioValue ?? MediaQuery .of (context).devicePixelRatio;
97
+ final boundary = findRenderObject as RenderRepaintBoundary ;
98
+ final context = _containerKey.currentContext;
99
+ var pixelRatioValue = pixelRatio;
100
+ if (pixelRatio == null ) {
101
+ if (context != null ) {
102
+ pixelRatioValue =
103
+ pixelRatio ?? MediaQuery .of (context).devicePixelRatio;
104
+ }
94
105
}
106
+ final image = await boundary.toImage (pixelRatio: pixelRatioValue ?? 1 );
107
+ return image;
108
+ } catch (exception) {
109
+ _logger.severe ('Failed to capture screenshot: $exception ' );
95
110
}
96
- final image = await boundary.toImage (pixelRatio: pixelRatio ?? 1 );
97
- return image;
111
+ return null ;
98
112
});
99
113
}
100
114
0 commit comments