很多应用都会这么操作,把一些图像进行缓存可以提升用户体验,也能减轻资源浪费,这里以cached_network_image为例。它可以将网络图像进行本地缓存,在需要的时候直接加载,提供了两个使用方法:
CachedNetworkImage(
imageUrl: "http://via.placeholder.com/350x150",
placeholder: (context, url) => CircularProgressIndicator(),
errorWidget: (context, url, error) => Icon(Icons.error),
),
使用以上代码的同学可能知道,在加载之前默认的loading会铺满整个视图,不太好看,所以我们还是可以修下:
CachedNetworkImage(
imageUrl: '${bannerList[index]['image']}',
placeholder: (context, url) => Container(
width: 130,
height: 80,
child: Center(
child: CircularProgressIndicator(
strokeWidth: 2,
),
),
),
errorWidget: (context, url, error) => Icon(Icons.error),
fit: BoxFit.cover,
),
CachedNetworkImage
可以直接使用,也可以通过ImageProvider使用。
Image(image: CachedNetworkImageProvider(url))
如果您想同时拥有占位符功能和要在另一个窗口小部件中使用imageprovider,则可以提供imageBuilder:
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),
),
如果你使用了CachedNetworkImageProvider
的话,错误信息如:
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
则如下:
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,
==>我希望该库的用户确保网址完全有效,
这是理想的状态,但如果图片资源涉及到第三方存储,这就难免出现意外。请求出错的情况包括但不限于:
这样一看,处理起来确实麻烦,因为有时间差,即使能从返回信息来处理,但回调超时体验上就会造成体验不好。如果是能有效控制维护的数据和API当然可以像官方所说的一样,确保图片资源有效,不管是增删还是301跳转等都好处理,但这不现实。
问题还没解决,有最新进展会继续更……
precacheImage(
CachedNetworkImageProvider(image),
context, onError: (e, stackTrace) {
print(('Image failed to load with error:$e'));
setState(() {
imgCheck = false;
});
});
使用precacheImage
方法进行图片预加载,该方法有一个回调onError
,它会读取缓存中是否存在err,如果存在,你就可以做点什么了?经测试该方法能解决图像404和网络异常导致的问题。但对于图片URL存在真实有效,却不是一个有效图片这个没法判断。
https://www.cnblogs.com/maqingyuan/p/13717437.html
https://www.jb51.net/article/213516.htm