Skip to content

Commit efedf51

Browse files
authored
[vector_graphics] Don't report a handled loading error as unhandled (flutter#13315)
`_VectorGraphicWidgetState._loadPicture` registers `result.whenComplete(...)` to clear `_pendingPictures`, and drops the future that `whenComplete` returns. That future completes with `result`'s error and nothing listens to it, so every asynchronous loading failure — an asset whose fetch fails on web when the network drops, for instance — is reported to the zone as unhandled, even though `_loadAssetBytes` catches the same error and `errorBuilder` shows it. Crash reporters record these as fatal. The dropped future is now `.ignore()`d. `result` itself is unchanged, so `_loadAssetBytes` still sees the error and shows `errorBuilder` as before. The existing `can handle errors from bytes loader` test didn't catch this because its loader throws synchronously, before `whenComplete` is reached. The new test uses a loader whose `loadBytes` is `async`; it fails without the fix with the `UnimplementedError` reported by the test framework. Fixes flutter/flutter#194705. ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent f2c6798 commit efedf51

4 files changed

Lines changed: 39 additions & 3 deletions

File tree

‎packages/vector_graphics/CHANGELOG.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
## NEXT
1+
## 1.2.4
22

3+
* Fixes an unhandled exception reported for every asynchronous loading failure,
4+
such as an asset that fails to load, even when `errorBuilder` handles it.
35
* Updates minimum supported SDK version to Flutter 3.41/Dart 3.11.
46
* Removes unnecessary Material imports.
57
* Fixes deprecation lints.

‎packages/vector_graphics/lib/src/vector_graphics.dart‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,12 @@ class _VectorGraphicWidgetState extends State<VectorGraphic> {
382382
return _PictureData(pictureInfo, 0, key);
383383
});
384384
_pendingPictures[key] = result;
385+
// The future whenComplete returns carries the same error as result, and
386+
// nothing listens to it: ignore it, or every failed load is reported as
387+
// unhandled even though the caller handles result's error.
385388
result.whenComplete(() {
386389
_pendingPictures.remove(key);
387-
});
390+
}).ignore();
388391
return result;
389392
}
390393

‎packages/vector_graphics/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: vector_graphics
22
description: A vector graphics rendering package for Flutter using a binary encoding.
33
repository: https://github.com/flutter/packages/tree/main/packages/vector_graphics
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+vector_graphics%22
5-
version: 1.2.3
5+
version: 1.2.4
66

77
environment:
88
sdk: ^3.11.0

‎packages/vector_graphics/test/vector_graphics_test.dart‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,28 @@ void main() {
773773
expect(find.text('Error is handled'), findsOneWidget);
774774
expect(tester.takeException(), isNull);
775775
});
776+
777+
testWidgets('VectorGraphicsWidget handles an asynchronous error from bytes loader', (
778+
WidgetTester tester,
779+
) async {
780+
await tester.pumpWidget(
781+
VectorGraphic(
782+
loader: const AsyncThrowingBytesLoader(),
783+
width: 100,
784+
height: 100,
785+
errorBuilder: (BuildContext context, Object error, StackTrace stackTrace) {
786+
return const Directionality(
787+
textDirection: TextDirection.ltr,
788+
child: Text('Error is handled'),
789+
);
790+
},
791+
),
792+
);
793+
await tester.pumpAndSettle();
794+
795+
expect(find.text('Error is handled'), findsOneWidget);
796+
expect(tester.takeException(), isNull);
797+
});
776798
}
777799

778800
class TestBundle extends Fake implements AssetBundle {
@@ -848,3 +870,12 @@ class ThrowingBytesLoader extends BytesLoader {
848870
throw UnimplementedError('Test exception');
849871
}
850872
}
873+
874+
class AsyncThrowingBytesLoader extends BytesLoader {
875+
const AsyncThrowingBytesLoader();
876+
877+
@override
878+
Future<ByteData> loadBytes(BuildContext? context) async {
879+
throw UnimplementedError('Test exception');
880+
}
881+
}

0 commit comments

Comments
 (0)