Flutter how to handle Image.network error (like 404 or wrong url)
Solution 1:
I have handled the network image issue related to 404 by using an errorBuilder.
Image.network('Your image url...',
errorBuilder: (BuildContext context, Object exception, StackTrace stackTrace) {
return Text('Your error widget...');
},
),
errorBuilder property
Solution 2:
UPDATE: Look at the new accurate way which uses an inbuilt method, answered by @Niraj Phutane here which requires no plugins.
outdated old answer (before errorbuilder existed)
I recommend using cached_network_image which gives an option to add a placeholder image and also an error widget in case of a 404 or 403.
CachedNetworkImage(
imageUrl: "http://via.placeholder.com/350x150",
placeholder: (context, url) => new CircularProgressIndicator(),
errorWidget: (context, url, error) => new Icon(Icons.error),
),
Solution 3:
Instead of Network.image
use NetworkImageWithRetry
https://pub.dartlang.org/documentation/flutter_image/latest/
Example:
var avatar = Image(
image: NetworkImageWithRetry('http://example.com/avatars/123.jpg'),
);