Add errorBuilder to Image widget#52481
Conversation
| ); | ||
|
|
||
| /// Signature for the method that is called when loading an image results in an | ||
| /// error. |
There was a problem hiding this comment.
include a link to [Image.errorBuilder] in this doc comment?
There was a problem hiding this comment.
Done. Reworded it a bit as well to indicate what it does.
|
|
||
| /// A builder function that is called if an error occurs during image loading. | ||
| /// | ||
| /// If this builder is not provided, any exceptions will be reporetd to |
| /// A builder function that is called if an error occurs during image loading. | ||
| /// | ||
| /// If this builder is not provided, any exceptions will be reporetd to | ||
| /// [FlutterError.onError]. If it is provided, the caller should either handle |
There was a problem hiding this comment.
This should mention that handling means returning a replacement widget that will render instead of the image.
| /// child: Image.network( | ||
| /// 'https://example.does.not.exist/image.jpg', | ||
| /// errorBuilder: (BuildContext context, Object exception, StackTrace stackTrace) { | ||
| /// print('An error occurred loading "https://example.does.not.exist/image.jpg": $exception'); |
There was a problem hiding this comment.
I would just remove the print. In general it wouldn't be good practice to have one here in an app and examples should show good practice.
There was a problem hiding this comment.
Replaced the print with a comment indicating how you might use this for analytics purposes.
| /// error. | ||
| typedef ImageErrorWidgetBuilder = Widget Function( | ||
| BuildContext context, | ||
| Object error, |
There was a problem hiding this comment.
why Object instead of dynamic here? ImageErrorListener types this as dynamic.
There was a problem hiding this comment.
I think ImageErrorListener should probably be Object, and I'm trying to avoid introducing dynamic to another place in the framework. In particular for this, this gets surfaced at a higher level in the API, and it would be good to avoid people interacting with this object without casting it to something proper. In particular, if they call some method on the dynamic object without casting it, it will prevent tree shaking for all types that have that method :\
| /// error. | ||
| typedef ImageErrorWidgetBuilder = Widget Function( | ||
| BuildContext context, | ||
| Object error, |
|
awesome!!!! |
|
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of |
Description
If resolving the image for an
Imagewidget results in an exception, we don't provide a good way to catch and try to handle that exception today. This adds anerrorBuildercallback to theImagewidget so that callers can decide to provide a replacement widget and/or do logging or analytics on failures./cc @escamoteur @slightfoot
Related Issues
Fixes #44579 (again :D)
Tests
I added the following tests:
Checklist
Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes (
[x]). This will ensure a smooth and quick review process.///).flutter analyze --flutter-repo) does not report any problems on my PR.Breaking Change
Did any tests fail when you ran them? Please read Handling breaking changes.