首页 > 解决方案 > Flutter中没有连接时如何处理CachedNetworkImage的错误?

问题描述

当我的控制台中没有互联网连接并且我找不到处理它的方法时,我收到此错误:

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)

我缓存的图像代码:

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) =>
            Icon(Icons.error, color: kAccentColor),
      ),
    );
  }
}

我尝试使用 try/catch,但仍然出现错误。

标签: flutterdart

解决方案


推荐阅读