Sometimes when I call this code:
Widget displayImage(BuildContext context, MyBook book) {
if (book.coverUrl != null) {
try {
return Image.network(book.coverUrl!,
errorBuilder: (context, error, stackTrace) {
return Text('No nimage');
});
} on Exception {
return Text('No nImage');
}
} else {
return Text('No nImage');
}
}
I receive the message
════════ Exception caught by image resource service ══════════════════
Invalid argument(s): No host specified in
URI file:///home/alan/FlutterProjects/books/null
═══════════════════════════════════════════════════════
(file:///home/alan/FlutterProjects/books/
is my app’s path. book.coverUrl
is a String
.)
The online image displays properly, despite the Exception message.
What causes this? Do I need to fix it?
EDIT:
The end of the stack track also includes the message:
Image provider: NetworkImage("null", scale: 1.0)
Image key: NetworkImage("null", scale: 1.0)
2
Answers
The url seems invalid, I will prefer short way like
You need to check image URL:
image URL: ensure that, it’s preceded by
http://
orhttps://
as a valid URLif not, just append that string by
https://
.And your condition should be: