Skip to content

Commit 175c0a5

Browse files
author
gregor
committed
used preCacheImage to catch errors while image loading
1 parent d354604 commit 175c0a5

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

lib/html_parser.dart

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ class HtmlRichTextParser extends StatelessWidget {
260260
);
261261

262262
// ignore the top level "body"
263-
body.nodes.forEach((dom.Node node) => _parseNode(node, parseContext));
263+
body.nodes
264+
.forEach((dom.Node node) => _parseNode(node, parseContext, context));
264265
// _parseNode(body, parseContext);
265266

266267
// filter out empty widgets
@@ -297,7 +298,8 @@ class HtmlRichTextParser extends StatelessWidget {
297298
// function can add child nodes to the parent if it should
298299
//
299300
// each iteration creates a new parseContext as a copy of the previous one if it needs to
300-
void _parseNode(dom.Node node, ParseContext parseContext) {
301+
void _parseNode(
302+
dom.Node node, ParseContext parseContext, BuildContext buildContext) {
301303
// TEXT ONLY NODES
302304
// a text only node is a child of a tag with no inner html
303305
if (node is dom.Text) {
@@ -619,9 +621,27 @@ class HtmlRichTextParser extends StatelessWidget {
619621
if (node.attributes['src'] != null) {
620622
if (node.attributes['src'].startsWith("data:image") &&
621623
node.attributes['src'].contains("base64,")) {
624+
precacheImage(
625+
MemoryImage(
626+
base64.decode(
627+
node.attributes['src'].split("base64,")[1].trim(),
628+
),
629+
),
630+
buildContext,
631+
onError: (_, stacktrace) {
632+
print(stacktrace);
633+
},
634+
);
622635
parseContext.rootWidgetList.add(Image.memory(base64.decode(
623636
node.attributes['src'].split("base64,")[1].trim())));
624637
} else {
638+
precacheImage(
639+
NetworkImage(node.attributes['src']),
640+
buildContext,
641+
onError: (_, stacktrace) {
642+
print(stacktrace);
643+
},
644+
);
625645
parseContext.rootWidgetList
626646
.add(Image.network(node.attributes['src']));
627647
}
@@ -738,7 +758,7 @@ class HtmlRichTextParser extends StatelessWidget {
738758
}
739759

740760
node.nodes.forEach((dom.Node childNode) {
741-
_parseNode(childNode, nextContext);
761+
_parseNode(childNode, nextContext, buildContext);
742762
});
743763
}
744764
}

0 commit comments

Comments
 (0)