skip to Main Content

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


  1. The url seems invalid, I will prefer short way like

    Widget displayImage(BuildContext context, MyBook book) {
      return Image.network(
        "${book.coverUrl}",// will handle by the errorBuilder
        errorBuilder: (context, error, stackTrace) {
          return Text('No nimage');
        },
      );
    }
    
    
    Login or Signup to reply.
  2. You need to check image URL:

    • image URL: ensure that, it’s preceded by http:// or https:// as a valid URL

      if not, just append that string by https://.

    And your condition should be:

    if (book.coverUrl != null && book.coverUrl != 'null'){
     // return it here
    } 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search