Flutter cached_network_image图片缓存异常/加载失败优化
- 发表于
- flutter
很多应用都会这么操作,把一些图像进行缓存可以提升用户体验,也能减轻资源浪费,这里以cached_network_image为例。它可以将网络图像进行本地缓存,在需要的时候直接加载,提供了两个使用方法:
1 2 3 4 5 |
CachedNetworkImage( imageUrl: "http://via.placeholder.com/350x150", placeholder: (context, url) => CircularProgressIndicator(), errorWidget: (context, url, error) => Icon(Icons.error), ), |
CachedNetworkImage
可以直接使用,也可以通过ImageProvider使用。
1 |
Image(image: CachedNetworkImageProvider(url)) |
如果您想同时拥有占位符功能和要在另一个窗口小部件中使用imageprovider,则可以提供imageBuilder:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
CachedNetworkImage( imageUrl: "http://via.placeholder.com/200x150", imageBuilder: (context, imageProvider) => Container( decoration: BoxDecoration( image: DecorationImage( image: imageProvider, fit: BoxFit.cover, colorFilter: ColorFilter.mode(Colors.red, BlendMode.colorBurn)), ), ), placeholder: (context, url) => CircularProgressIndicator(), errorWidget: (context, url, error) => Icon(Icons.error), ), |
图像加载失败:404,403等
如果你使用了CachedNetworkImageProvider
的话,错误信息如:
1 2 3 4 5 6 7 8 9 10 11 12 |
I/flutter (21047): CacheManager: Failed to download file from https://pic.xx.com/28000.jpg with error: I/flutter (21047): SocketException: Failed host lookup: 'pic.xx.com' (OS Error: No address associated with hostname, errno = 7) I/flutter (21047): CacheManager: Failed to download file from https://pic.xx.com/23730.jpg with error: I/flutter (21047): SocketException: Failed host lookup: 'pic.aotorun.com' (OS Error: No address associated with hostname, errno = 7) I/flutter (21047): CacheManager: Failed to download file from with error: I/flutter (21047): Invalid argument(s): No host specified in URI I/flutter (21047): CacheManager: Failed to download file from with error: I/flutter (21047): Invalid argument(s): No host specified in URI I/flutter (21047): CacheManager: Failed to download file from https://i1.xx.com/623946/4c71965fac192ebe.jpg with error: I/flutter (21047): HttpException: No valid statuscode. Statuscode was 404 I/flutter (21047): CacheManager: Failed to download file from https://i1.xx.com/623946/4c71965fac192ebe.jpg with error: I/flutter (21047): HttpException: No valid statuscode. Statuscode was 404 |
如果是CachedNetworkImage
则如下:
1 2 3 4 |
I/flutter (21047): Path: I/flutter (21047): /data/user/0/com.example.xx/cache/libCachedImageData/68b03f20-1fa4-11ea-9f10-49083fd8fc8f.jpeg I/flutter (21047): ════════════════════════════════════════════════════════════════════════════════════════════════════ I/flutter (21047): Another exception was thrown: Exception: Could not instantiate image codec. |
如何解决?
从文档提供的资料来看,没有对应的解决方案,在issues中我看到大量提出该问题的疑惑,都是在问如何解决的,但目前来看也没有相应的解决方案。其中看到最新的维护人员的回复:
I'd expect that the user of this library makes sure that the url is completely valid,
==>我希望该库的用户确保网址完全有效,
这是理想的状态,但如果图片资源涉及到第三方存储,这就难免出现意外。请求出错的情况包括但不限于:
- 404,图片不存在
- 403,无权限访问
- 无效图片资源,比如占位符并不是有效图像
- 网络不通
- ……
这样一看,处理起来确实麻烦,因为有时间差,即使能从返回信息来处理,但回调超时体验上就会造成体验不好。如果是能有效控制维护的数据和API当然可以像官方所说的一样,确保图片资源有效,不管是增删还是301跳转等都好处理,但这不现实。
问题还没解决,有最新进展会继续更……
解决方法一:
1 2 3 4 5 6 7 8 |
precacheImage( CachedNetworkImageProvider(image), context, onError: (e, stackTrace) { print(('Image failed to load with error:$e')); setState(() { imgCheck = false; }); }); |
使用precacheImage
方法进行图片预加载,该方法有一个回调onError
,它会读取缓存中是否存在err,如果存在,你就可以做点什么了?经测试该方法能解决图像404和网络异常导致的问题。但对于图片URL存在真实有效,却不是一个有效图片这个没法判断。
继续。。。
原文连接
的情况下转载,若非则不得使用我方内容。