-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Add errorBuilder to Image widget #52481
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
Conversation
| ); | ||
|
|
||
| /// Signature for the method that is called when loading an image results in an | ||
| /// error. |
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.
include a link to [Image.errorBuilder] in this doc comment?
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.
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 |
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.
spelling: reporetd
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.
Whups. done.
| /// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should mention that handling means returning a replacement widget that will render instead of the image.
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.
Done
| /// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why Object instead of dynamic here? ImageErrorListener types this as dynamic.
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.
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 :\
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.
Sounds good.
goderbauer
left a comment
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.
LGTM
| /// error. | ||
| typedef ImageErrorWidgetBuilder = Widget Function( | ||
| BuildContext context, | ||
| Object error, |
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.
Sounds good.
|
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.