4

I am building an app in which I get image url from api and show them to user. This is my code:

 Image.network(
               news.urlToImage ??
               defaultPlaceholderImageUrl,
                      ),

Api sometimes doesn't provide url, so I displaye placeholder image, this part works fine. But I get the following error with one of the provided urls:

════════ Exception caught by image resource service ════════════════════════════════════════════════
Invalid argument(s): No host specified in URI https:////m.files.bbci.co.uk/modules/bbc-morph-sport-seo-meta/1.20.8/images/bbc-sport-logo.png

That link works in browser. But not in app. How can I solve this?

Tugay
  • 2,057
  • 5
  • 17
  • 32
  • 1
    `https:////` there is 4 slash, could you check if this is creating the issue? – towhid Oct 24 '20 at 15:22
  • @towhid that was causing the problem, thanks. But do I need to manually correct that? – Tugay Oct 24 '20 at 15:56
  • 1
    if you have access to the database or somehow you can change that then you must do that. Otherwise, you have to fix it manually `replaceAll('////', '//')` – towhid Oct 25 '20 at 05:16
  • Well, I can't access, it is coming from API. But I would like to know that, how can use try-catch to prevent these kinds of errors and show user default image, if something us wrong – Tugay Oct 25 '20 at 10:18
  • 1
    as far as I know, there is no native way to do it using `Image.network()`. But you can get help from this post: https://stackoverflow.com/q/52568872/6413387 – towhid Oct 25 '20 at 18:39
  • @towhid thank you, man. You are one big life saver – Tugay Oct 25 '20 at 19:29
  • write these as answer, and I will accept it – Tugay Oct 25 '20 at 19:36

1 Answers1

5

The problem was in the URL https:////m.files.bbci..... it has 4 / which was an invalid URL format for Image.network() widget. So far there is no native way to catch the exception, but you can use different libraries. You can learn more about the issue and possible solutions from this post.

Flutter how to handle Image.network error (like 404 or wrong url)

towhid
  • 2,778
  • 5
  • 18
  • 28