@@ -25,24 +25,68 @@ final ImageBuilder kDefaultImageBuilder = (
2525 double ? height,
2626) {
2727 if (uri.scheme == 'http' || uri.scheme == 'https' ) {
28- return Image .network (uri.toString (), width: width, height: height);
28+ return Image .network (
29+ uri.toString (),
30+ width: width,
31+ height: height,
32+ errorBuilder: kDefaultImageErrorWidgetBuilder,
33+ );
2934 } else if (uri.scheme == 'data' ) {
3035 return _handleDataSchemeUri (uri, width, height);
3136 } else if (uri.scheme == 'resource' ) {
32- return Image .asset (uri.path, width: width, height: height);
37+ return Image .asset (
38+ uri.path,
39+ width: width,
40+ height: height,
41+ errorBuilder: kDefaultImageErrorWidgetBuilder,
42+ );
3343 } else {
34- final Uri fileUri = imageDirectory != null
35- ? Uri .parse (p.join (imageDirectory, uri.toString ()))
36- : uri;
44+ final Uri fileUri;
45+
46+ if (imageDirectory != null ) {
47+ try {
48+ fileUri = Uri .parse (p.join (imageDirectory, uri.toString ()));
49+ } catch (error, stackTrace) {
50+ // Handle any invalid file URI's.
51+ return Builder (
52+ builder: (BuildContext context) {
53+ return kDefaultImageErrorWidgetBuilder (context, error, stackTrace);
54+ },
55+ );
56+ }
57+ } else {
58+ fileUri = uri;
59+ }
60+
3761 if (fileUri.scheme == 'http' || fileUri.scheme == 'https' ) {
38- return Image .network (fileUri.toString (), width: width, height: height);
62+ return Image .network (
63+ fileUri.toString (),
64+ width: width,
65+ height: height,
66+ errorBuilder: kDefaultImageErrorWidgetBuilder,
67+ );
3968 } else {
4069 final String src = p.join (p.current, fileUri.toString ());
41- return Image .network (src, width: width, height: height);
70+ return Image .network (
71+ src,
72+ width: width,
73+ height: height,
74+ errorBuilder: kDefaultImageErrorWidgetBuilder,
75+ );
4276 }
4377 }
4478};
4579
80+ /// A default error widget builder for handling image errors.
81+ // ignore: prefer_function_declarations_over_variables
82+ final ImageErrorWidgetBuilder kDefaultImageErrorWidgetBuilder = (
83+ BuildContext context,
84+ Object error,
85+ StackTrace ? stackTrace,
86+ ) {
87+ return const SizedBox ();
88+ };
89+
4690/// A default style sheet generator.
4791final MarkdownStyleSheet Function (BuildContext , MarkdownStyleSheetBaseTheme ? )
4892// ignore: prefer_function_declarations_over_variables
@@ -72,6 +116,7 @@ Widget _handleDataSchemeUri(
72116 uri.data! .contentAsBytes (),
73117 width: width,
74118 height: height,
119+ errorBuilder: kDefaultImageErrorWidgetBuilder,
75120 );
76121 } else if (mimeType.startsWith ('text/' )) {
77122 return Text (uri.data! .contentAsString ());
0 commit comments