-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[vector_graphics] handle errors from bytes loader #8080
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'dart:async'; | ||
import 'dart:math' as math; | ||
import 'dart:ui' as ui; | ||
|
||
|
@@ -315,14 +316,14 @@ class _VectorGraphicWidgetState extends State<VectorGraphic> { | |
void didChangeDependencies() { | ||
locale = Localizations.maybeLocaleOf(context); | ||
textDirection = Directionality.maybeOf(context); | ||
_loadAssetBytes(); | ||
unawaited(_loadAssetBytes()); | ||
super.didChangeDependencies(); | ||
} | ||
|
||
@override | ||
void didUpdateWidget(covariant VectorGraphic oldWidget) { | ||
if (oldWidget.loader != widget.loader) { | ||
_loadAssetBytes(); | ||
unawaited(_loadAssetBytes()); | ||
} | ||
super.didUpdateWidget(oldWidget); | ||
} | ||
|
@@ -358,12 +359,6 @@ class _VectorGraphicWidgetState extends State<VectorGraphic> { | |
textDirection: key.textDirection, | ||
clipViewbox: key.clipViewbox, | ||
loader: loader, | ||
onError: (Object error, StackTrace? stackTrace) { | ||
return _handleError( | ||
error, | ||
stackTrace, | ||
); | ||
}, | ||
); | ||
}).then((PictureInfo pictureInfo) { | ||
return _PictureData(pictureInfo, 0, key); | ||
|
@@ -376,13 +371,17 @@ class _VectorGraphicWidgetState extends State<VectorGraphic> { | |
} | ||
|
||
void _handleError(Object error, StackTrace? stackTrace) { | ||
if (!mounted) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this function is invoked after the future has been initiated, the widget could be unmounted. |
||
return; | ||
} | ||
|
||
setState(() { | ||
_error = error; | ||
_stackTrace = stackTrace; | ||
}); | ||
} | ||
|
||
void _loadAssetBytes() { | ||
Future<void> _loadAssetBytes() async { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I initially tried using a Since async work in a |
||
// First check if we have an avilable picture and use this immediately. | ||
final Object loaderKey = widget.loader.cacheKey(context); | ||
final _PictureKey key = | ||
|
@@ -398,7 +397,9 @@ class _VectorGraphicWidgetState extends State<VectorGraphic> { | |
} | ||
// If not, then check if there is a pending load. | ||
final BytesLoader loader = widget.loader; | ||
_loadPicture(context, key, loader).then((_PictureData data) { | ||
|
||
try { | ||
final _PictureData data = await _loadPicture(context, key, loader); | ||
data.count += 1; | ||
|
||
// The widget may have changed, requesting a new vector graphic before | ||
|
@@ -407,14 +408,18 @@ class _VectorGraphicWidgetState extends State<VectorGraphic> { | |
_maybeReleasePicture(data); | ||
return; | ||
} | ||
|
||
if (data.count == 1) { | ||
_livePictureCache[key] = data; | ||
} | ||
|
||
setState(() { | ||
_maybeReleasePicture(_pictureInfo); | ||
_pictureInfo = data; | ||
}); | ||
}); | ||
} catch (error, stackTrace) { | ||
_handleError(error, stackTrace); | ||
} | ||
} | ||
|
||
static final bool _webRenderObject = useHtmlRenderObject(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error handler has been moved higher up, so this one should not be needed anymore.