Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

dart - How to handle error by CachedNetworkImage when there is no connection in Flutter?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...