diff --git a/lib/ui/painting.dart b/lib/ui/painting.dart index eb507943b8ad1..95b742f180e43 100644 --- a/lib/ui/painting.dart +++ b/lib/ui/painting.dart @@ -1939,12 +1939,13 @@ class Codec extends NativeFieldWrapperClass2 { final Completer completer = Completer.sync(); final String? error = _getNextFrame((_Image? image, int durationMilliseconds) { if (image == null) { - throw Exception('Codec failed to produce an image, possibly due to invalid image data.'); + completer.completeError(Exception('Codec failed to produce an image, possibly due to invalid image data.')); + } else { + completer.complete(FrameInfo._( + image: Image._(image), + duration: Duration(milliseconds: durationMilliseconds), + )); } - completer.complete(FrameInfo._( - image: Image._(image), - duration: Duration(milliseconds: durationMilliseconds), - )); }); if (error != null) { throw Exception(error); diff --git a/testing/dart/codec_test.dart b/testing/dart/codec_test.dart index 961f54f4ace6d..649aba4562eab 100644 --- a/testing/dart/codec_test.dart +++ b/testing/dart/codec_test.dart @@ -34,6 +34,18 @@ void main() { ); }); + test('getNextFrame fails with invalid data', () async { + Uint8List data = await _getSkiaResource('flutter_logo.jpg').readAsBytes(); + data = Uint8List.view(data.buffer, 0, 4000); + final ui.Codec codec = await ui.instantiateImageCodec(data); + try { + await codec.getNextFrame(); + fail('exception not thrown'); + } catch(e) { + expect(e, exceptionWithMessage('Codec failed')); + } + }); + test('nextFrame', () async { final Uint8List data = await _getSkiaResource('test640x479.gif').readAsBytes(); final ui.Codec codec = await ui.instantiateImageCodec(data);