GreyScreenOfMeh
GreyScreenOfMeh

Reputation: 273

Flutter not loading images that failed to load the first time

Switch airplane on mode and navigate to a StatefulWidget that includes a NetworkImage. The image fails to load since there is no connectivity. Now switch on airplane mode off. The image doesn't load. Navigating to another view and back again also doesn't help. Minimizing the app doesn't help, but restarting it completely does.

How would you resolve this? How do I make Flutter try to reload the image connectivity is regained?

Upvotes: 0

Views: 394

Answers (2)

Siddharth Sogani
Siddharth Sogani

Reputation: 349

After 2 days of trying all image plugins and answers on stackoverflow. I realized the error goes away when I go back and come back to same screen. So I retried loading images 3 times.

                          CachedNetworkImage(
                          key: ValueKey(imageUrl
                              .split),
                          placeholder: (context, url) => Padding(
                              padding: EdgeInsets.all(
                                  MediaQuery.of(context).size.width * 0.2),
                              child: CircularProgressIndicator()),
                          errorWidget: (context, url, error) => CachedNetworkImage(
                              key: ValueKey(imageUrlsplit +
                                  'retry1'),
                              placeholder: (context, url) =>
                                  Padding(padding: EdgeInsets.all(MediaQuery.of(context).size.width * 0.2), child: CircularProgressIndicator()),
                              errorWidget: (context, url, error) => CachedNetworkImage(key: ValueKey(imageUrlsplit + 'retry2'), placeholder: (context, url) => Padding(padding: EdgeInsets.all(MediaQuery.of(context).size.width * 0.2), child: CircularProgressIndicator()), errorWidget: (context, url, error) => Text(error.toString()), imageUrl: imageUrlsplit),
                              imageUrl: imageUrlsplit),
                          imageUrl: imageUrlsplit)

Upvotes: 0

Albert Lardizabal
Albert Lardizabal

Reputation: 6846

Add the flutter_image plugin and use NetworkImageWithRetry.

Upvotes: 2

Related Questions