如何解决Image.network() 失败时无法捕获 NetworkImageLoadException
我使用 Image.network() 显示来自 URL 的图像 这就是我使用它的方式
Image image = Image.network(
_auth.currentUser!.photoURL!,width: 100.getWidth(context),height: 100.getWidth(context),frameBuilder: (context,child,frame,wasSynchronouslyLoaded) {
return wasSynchronouslyLoaded
? child
: _profileImagePlaceholder(context);
},loadingBuilder: (context,loadingProgress) {
return loadingProgress == null
? child
: _profileImagePlaceholder(context);
},errorBuilder: (context,error,stackTrace) {
return _profileImagePlaceholder(context);
},);
但即使我设置了 errorBuilder 甚至用 try/catch 包装了整个事情
这个NetworkImageLoadException
仍然显示
完全异常
The following NetworkImageLoadException was thrown resolving an image codec:
HTTP request failed,statusCode: 403,When the exception was thrown,this was the stack:
#0 NetworkImage._loadAsync (package:flutter/src/painting/_network_image_io.dart:99:9)
<asynchronous suspension>
...
Image provider:
NetworkImage("https://firebasestorage.googleapis.com/v0/b/biddee-co.appspot.com/o/profiles%2FdefaultProfile.png?alt=media&token=a4a99031-aabd-4597-b075-77ecb2d3e594",scale: 1.0)
Image key:
NetworkImage("https://firebasestorage.googleapis.com/v0/b/biddee-co.appspot.com/o/profiles%2FdefaultProfile.png?alt=media&token=a4a99031-aabd-4597-b075-77ecb2d3e594",scale: 1.0)
解决方法
您可以为此使用 ErrorBuilder
*ngIf="selectedIndex==1"
查找文档 from here
,在package:flutter/src/painting/_network_image_io.dart
if (response.statusCode != HttpStatus.ok) {
// The network may be only temporarily unavailable,or the file will be
// added on the server later. Avoid having future calls to resolve
// fail to check the network again.
await response.drain<List<int>>();
throw image_provider.NetworkImageLoadException(statusCode: response.statusCode,uri: resolved);
}
该注释是导致异常的原因。
网络可能只是暂时不可用,或者稍后会在服务器上添加文件。 避免以后调用解决再次检查网络失败的问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。