Skip to content

Commit 913b99e

Browse files
[vector_graphics] handle errors from bytes loader (flutter#8080)
This PR updates the VectorGraphicsWidget to handle errors from the underlying bytes loader using the existing error handling in its implementation. *List which issues are fixed by this PR. You must list at least one issue.* Fixes flutter/flutter#158862
1 parent 013cc24 commit 913b99e

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

packages/vector_graphics/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.1.15
2+
3+
* Updates error handling in VectorGraphicWidget to handle errors when the bytes of the graphic cannot be loaded.
4+
15
## 1.1.14
26

37
* Relaxes dependency constraint on vector_graphics_codec.

packages/vector_graphics/lib/src/vector_graphics.dart

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'dart:async';
56
import 'dart:math' as math;
67
import 'dart:ui' as ui;
78

@@ -315,14 +316,14 @@ class _VectorGraphicWidgetState extends State<VectorGraphic> {
315316
void didChangeDependencies() {
316317
locale = Localizations.maybeLocaleOf(context);
317318
textDirection = Directionality.maybeOf(context);
318-
_loadAssetBytes();
319+
unawaited(_loadAssetBytes());
319320
super.didChangeDependencies();
320321
}
321322

322323
@override
323324
void didUpdateWidget(covariant VectorGraphic oldWidget) {
324325
if (oldWidget.loader != widget.loader) {
325-
_loadAssetBytes();
326+
unawaited(_loadAssetBytes());
326327
}
327328
super.didUpdateWidget(oldWidget);
328329
}
@@ -358,12 +359,6 @@ class _VectorGraphicWidgetState extends State<VectorGraphic> {
358359
textDirection: key.textDirection,
359360
clipViewbox: key.clipViewbox,
360361
loader: loader,
361-
onError: (Object error, StackTrace? stackTrace) {
362-
return _handleError(
363-
error,
364-
stackTrace,
365-
);
366-
},
367362
);
368363
}).then((PictureInfo pictureInfo) {
369364
return _PictureData(pictureInfo, 0, key);
@@ -376,13 +371,17 @@ class _VectorGraphicWidgetState extends State<VectorGraphic> {
376371
}
377372

378373
void _handleError(Object error, StackTrace? stackTrace) {
374+
if (!mounted) {
375+
return;
376+
}
377+
379378
setState(() {
380379
_error = error;
381380
_stackTrace = stackTrace;
382381
});
383382
}
384383

385-
void _loadAssetBytes() {
384+
Future<void> _loadAssetBytes() async {
386385
// First check if we have an avilable picture and use this immediately.
387386
final Object loaderKey = widget.loader.cacheKey(context);
388387
final _PictureKey key =
@@ -398,7 +397,9 @@ class _VectorGraphicWidgetState extends State<VectorGraphic> {
398397
}
399398
// If not, then check if there is a pending load.
400399
final BytesLoader loader = widget.loader;
401-
_loadPicture(context, key, loader).then((_PictureData data) {
400+
401+
try {
402+
final _PictureData data = await _loadPicture(context, key, loader);
402403
data.count += 1;
403404

404405
// The widget may have changed, requesting a new vector graphic before
@@ -407,14 +408,18 @@ class _VectorGraphicWidgetState extends State<VectorGraphic> {
407408
_maybeReleasePicture(data);
408409
return;
409410
}
411+
410412
if (data.count == 1) {
411413
_livePictureCache[key] = data;
412414
}
415+
413416
setState(() {
414417
_maybeReleasePicture(_pictureInfo);
415418
_pictureInfo = data;
416419
});
417-
});
420+
} catch (error, stackTrace) {
421+
_handleError(error, stackTrace);
422+
}
418423
}
419424

420425
static final bool _webRenderObject = useHtmlRenderObject();

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.1.14
5+
version: 1.1.15
66

77
environment:
88
sdk: ^3.4.0

packages/vector_graphics/test/vector_graphics_test.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,28 @@ void main() {
653653
expect(matrix.row0.x, -1);
654654
expect(matrix.row1.y, 1);
655655
});
656+
657+
testWidgets('VectorGraphicsWidget can handle errors from bytes loader',
658+
(WidgetTester tester) async {
659+
await tester.pumpWidget(
660+
VectorGraphic(
661+
loader: const ThrowingBytesLoader(),
662+
width: 100,
663+
height: 100,
664+
errorBuilder:
665+
(BuildContext context, Object error, StackTrace stackTrace) {
666+
return const Directionality(
667+
textDirection: TextDirection.ltr,
668+
child: Text('Error is handled'),
669+
);
670+
},
671+
),
672+
);
673+
await tester.pumpAndSettle();
674+
675+
expect(find.text('Error is handled'), findsOneWidget);
676+
expect(tester.takeException(), isNull);
677+
});
656678
}
657679

658680
class TestBundle extends Fake implements AssetBundle {
@@ -719,3 +741,12 @@ class TestBytesLoader extends BytesLoader {
719741
@override
720742
String toString() => 'TestBytesLoader: $source';
721743
}
744+
745+
class ThrowingBytesLoader extends BytesLoader {
746+
const ThrowingBytesLoader();
747+
748+
@override
749+
Future<ByteData> loadBytes(BuildContext? context) {
750+
throw UnimplementedError('Test exception');
751+
}
752+
}

0 commit comments

Comments
 (0)