I am getting this error when there is no internet connection in my console and I couldn't find a way to handle it:
Failed host lookup: 'via.placeholder.com' (OS Error: nodename nor servname provided, or not known, errno = 8)
When the exception was thrown, this was the stack
Image provider: CachedNetworkImageProvider("http://via.placeholder.com/350x150.", scale: 1.0)
Image key: CachedNetworkImageProvider("http://via.placeholder.com/350x150.", scale: 1.0): CachedNetworkImageProvider("http://via.placeholder.com/350x150.", scale: 1.0)
My cached image code:
class CachedImage extends StatelessWidget {
final String imageUrl;
final String id;
CachedImage({this.imageUrl, this.id});
@override
Widget build(BuildContext context) {
return Hero(
tag: id,
child: CachedNetworkImage(
fit: BoxFit.fill,
imageUrl: imageUrl,
placeholder: (context, url) => SizedBox(
height: 50,
width: 50,
child: FittedBox(fit: BoxFit.scaleDown, child: LoadingIndicator()),
),
errorWidget: (context, url, error) =>
Container(child: Icon(Icons.error, color: kTextBrownColor)),
),
);
}
}
I have tried with try/catch but still the error is showing up.
question from:
https://stackoverflow.com/questions/65601559/how-to-handle-error-by-cachednetworkimage-when-there-is-no-connection-in-flutter 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…