7

I'm using Image.network to load image from network. When I simply pass the url the image works fine. It will be displayed after few seconds. But when I use loadingBuilder and errorBuilder the images are loaded but not displayed. The circular progress I created inside the loadingBuilder completes and disappears but image is not shown.

Here code

Image.network(
      widget._imgLink,
      loadingBuilder: (context, child, loadingProgress) {
        return Center(
          child: CircularProgressIndicator(
            value: (loadingProgress != null)
                ? (loadingProgress.cumulativeBytesLoaded /
                    loadingProgress.expectedTotalBytes)
                : 0,
          ),
        );
      },
      errorBuilder: (context, error, stackTrace) {
        return Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text("Error loading"),
            FlatButton(onPressed: () {}, child: Text("Retry")),
          ],
        );
      },
    ),

How can I tell the widget to display the image after its loaded? Thank you.

3
  • you are not using child parameter at all in your loadingBuilder: (context, child, loadingProgress) { Commented Oct 31, 2020 at 6:56
  • Do I have to provide the child and how to use it? Commented Oct 31, 2020 at 10:02
  • see api.flutter.dev/flutter/widgets/Image/loadingBuilder.html Commented Oct 31, 2020 at 10:03

2 Answers 2

9

Returning child from loadingBuilder when loadingProgress is null will work. Sample:

loadingBuilder: (context, child, loadingProgress) {
        if(loadingProgress == null)
          return child;

        return Center(
          child: CircularProgressIndicator(
            value: (loadingProgress != null)
                ? (loadingProgress.cumulativeBytesLoaded /
                    loadingProgress.expectedTotalBytes)
                : 0,
          ),
        );
      },
Sign up to request clarification or add additional context in comments.

Comments

4

Use CachedNetworkImage which loads the image and provide error and loading widgets.

dependencies:
  cached_network_image: ^2.3.3

Code:

CachedNetworkImage(
  imageUrl: "http://via.placeholder.com/200x150",
  imageBuilder: (context, imageProvider) => Container(
    decoration: BoxDecoration(
      image: DecorationImage(
          image: imageProvider,
          fit: BoxFit.cover,
          colorFilter:
              ColorFilter.mode(Colors.red, BlendMode.colorBurn)),
    ),
  ),
  placeholder: (context, url) => CircularProgressIndicator(),
  errorWidget: (context, url, error) => Icon(Icons.error),
),

3 Comments

Thanks but why can't I use Image.network for the same as it provides these functionalities too.
you can use it, CachedNetworkImage is a more advanced way to way to show network image, it too provides caching.
CachedNetworkImage doesn't work in flutter-web so Image.Network still holds value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.