Skip to content

Image.network throws uncaught exception when providing valid url without an image  #81931

Description

@asmith20002

Summary:
I don't know why I can't catch Image.network exceptions. If I give Image.network an invalid URL, the errorBuilder kicks in, that's good but if I give it a valid URL with 404 response or non-image data, errorBuilder() doesn't seem to do anything. The app pauses because of the uncaught exception.

Details:
I made a new app with flutter create bug, this is my main.dart:

For the first test, I give it an invalid URL (AAABBBCCC)
This one works as expected. errorBuilder() is called and 'N/A' is printed on the screen.

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Network Image',
            ),
            MyImage(),
          ],
        ),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

class MyImage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    try {
      return Image.network(
        'AAABBBCCC', //url,
        errorBuilder: (BuildContext context, Object exception, StackTrace? stackTrace) {
          return Text('N/A');
        },
      );
    } catch (e) {
      print(e.toString());
    }

    return Container();
  }
}

photo_2021-05-06_00-03-04

Now a valid URL but with 404 response: (https://example.com/nothing)

class MyImage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    try {
      return Image.network(
        'https://example.com/nothing', //url,
        errorBuilder: (BuildContext context, Object exception, StackTrace? stackTrace) {
          return Text('N/A');
        },
      );
    } catch (e) {
      print(e.toString());
    }

    return Container();
  }
}

bug01

For the last try, a valid URL with data but invalid image data: (https://google.com/)

class MyImage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    try {
      return Image.network(
        'https://google.com/', //url,
        errorBuilder: (BuildContext context, Object exception, StackTrace? stackTrace) {
          return Text('N/A');
        },
      );
    } catch (e) {
      print(e.toString());
    }

    return Container();
  }
}

bug02

As you can see the app is pausing due to uncaught exceptions.

I've also tried FadeInImage() and CachedNetworkImage(), they both act the same as Image.network().

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel beta, 2.2.0-10.2.pre, on Microsoft Windows [Version 10.0.19041.928], locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[√] Chrome - develop for the web
[√] Android Studio (version 4.1.0)
[√] Android Studio
[√] Connected device (3 available)

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work lista: error messageError messages from the Flutter frameworka: imagesLoading, displaying, rendering imagescustomer: crowdAffects or could affect many people, though not necessarily a specific customer.found in release: 3.11Found to occur in 3.11found in release: 3.7Found to occur in 3.7frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onteam-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions