10

I'm using the cached_network_image library. If I set it up like this:

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    print('MyApp building');
    return MaterialApp(
      home: Scaffold(
        body: HomeWidget(),
      ),
    );
  }
}

class HomeWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        child: CachedNetworkImage(
          imageUrl: "https://example.com/image/whatever.png",
          placeholder: (context, url) => CircularProgressIndicator(),
          errorWidget: (context, url, error) => Icon(Icons.error),
       ),
      ),
    );
  }
}

with a URL that is invalid (either because it returns a 404 or the server refuses the connection), then my IDE freezes and gives one of the following errors:

HttpExceptionWithStatus (HttpException: Invalid statusCode: 404, uri = https://example.com/image/whatever.png)

or this (if I change it to a non-existent host):

SocketException (SocketException: Failed host lookup: 'ksdhfkajsdhfkashdfkadshfk.com' (OS Error: No address associated with hostname, errno = 7))

enter image description here

What I would expect is for the CachedNetworkImage widget to just show the errorWidget, but it doesn't.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393

3 Answers3

3

The creator of cached_network_image says (source):

If you have break on exceptions enabled the debugger stops, but that's because the dart VM doesn't always know whether an exception is caught or not. If you continue this still shouldn't freeze your app.

That was the key I needed. It is just the debugger stopping. You can hit the continue button or disable stopping on uncaught exceptions. The CachedNetworkImage widget will then show the errorWidget as expected.

In VS Code you can uncheck this in the bottom left of the debug pannel.

enter image description here

enter image description here

I believe there is a similar setting in Android studio based on this post.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • 13
    This actually does not solve the problem. We want the library to simply don't throw the error so the IDE won't stop. It's common to enable breakpoint on uncaught exceptions to debug, and disabling it in this cases is not a solution. Also, the error is propagated to crashlytics if you are using it, which is completely undesired. – Mateus Felipe Jul 26 '21 at 11:42
  • 1
    Still happening and google crashlytics show that app is crash. – Alex Aung Jun 14 '22 at 15:18
  • 1
    @MateusFelipe Having said that, do you have a solution? – Garrett Oct 31 '22 at 12:06
  • 1
    @Garret No, I haven't. – Mateus Felipe Oct 31 '22 at 17:00
1

I also experienced this issue, and could not find a flutter way to solve the 404 issue which crashes the app.

As a solution, I am using imagekit as a CDN and we can tell imagekit to display a default image if the original image is missing: https://docs.imagekit.io/features/default-images

Kng1230
  • 11
  • 2
0

The error persists with 3.2.0. The proposal by @kng1230 cannot work in all cases (I am not using a CDN).

Sunil Gupta
  • 666
  • 6
  • 20