Skip to content
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

[flutter_markdown] fix invalid URI's causing unhandled image errors #8058

Merged

Conversation

navaronbracke
Copy link
Contributor

@navaronbracke navaronbracke commented Nov 12, 2024

This PR adds an error builder for images, so that any errors from those are caught.

List which issues are fixed by this PR. You must list at least one issue.
Fixes flutter/flutter#158428

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

}
}
};

/// A default error widget builder for handling image errors.
// ignore: prefer_function_declarations_over_variables
final ImageErrorWidgetBuilder kDefaultImageErrorWidgetBuilder = (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The long term fix would be to provide an image error builder, next to the MarkdownWidget.imageBuilder, do I do that from the get-go? Or do we change the API for the imageBuilder to accept an error and stack trace?

return Image.file(File.fromUri(fileUri), width: width, height: height);
try {
return Image.file(
File.fromUri(fileUri),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately File.fromUri throws, so the errorBuilder is not invoked in that case

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be fixed by setting the value to a variable then returning it. I'm not 100% certain on that though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if that would work? Per the docs in https://api.dart.dev/dart-io/File/File.fromUri.html

If uri cannot reference a file this throws [UnsupportedError](https://api.dart.dev/dart-core/UnsupportedError-class.html)

This aligns with the second error reported in flutter/flutter#158428

Unsupported operation: Cannot extract a file path from a ttps URI

So that's why I added a try/catch here, since the Image.file() constructor won't be able to invoke its errorBuilder.

Copy link
Contributor

@domesticmouse domesticmouse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stuartmorgan stuartmorgan added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 26, 2024
@auto-submit auto-submit bot merged commit 2033119 into flutter:main Nov 26, 2024
77 checks passed
@navaronbracke navaronbracke deleted the fix_invalid_url_markdown_error branch November 26, 2024 21:03
WenHaozhan pushed a commit to WenHaozhan/packages that referenced this pull request Nov 27, 2024
…lutter#8058)

This PR adds an error builder for images, so that any errors from those are caught.

*List which issues are fixed by this PR. You must list at least one issue.*
Fixes flutter/flutter#158428
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App p: flutter_markdown
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[flutter_markdown] Invalid image URI crashes the app (instead of just showing an error at the image area)
4 participants