我有这样的东西:
try {
final image = NetworkImageWithRetry(url,
fetchStrategy: FetchStrategyBuilder(
timeout: Duration(seconds: 2),
transientHttpStatusCodePredicate: (code) {
return code == null ||
FetchStrategyBuilder
.defaultTransientHttpStatusCodePredicate(code);
}).build()
);
return Padding(
padding: EdgeInsets.all(10.0),
child: Image(
image: image,
),
);
} catch FetchFailure {
print('exception!');
return Container();
}
对于不存在的图片,图片尝试多次加载,总是抛出:
EXCEPTION CAUGHT BY PACKAGE:FLUTTER_IMAGE
The following FetchFailure was thrown NetworkImageWithRetry failed to load
我如何捕捉到它?我试过 catch Exception
、catch FetchFailure
、catch (error)
。
我也尝试过根据错误代码返回一个小部件,但是 FetchStrategyBuilder
要我返回一个 bool 值。
最佳答案
您无法捕获 FetchFailure 错误的原因是因为它已经在 flutter_image 中捕获了.您收到的错误消息中指出了这一点:EXCEPTION CAUGHT BY PACKAGE:FLUTTER_IMAGE
您能否提供更多详细信息,说明您 try catch 异常的原因?如果这样做的原因是您试图显示占位符图像以防图像加载失败,您可以使用 FadeInImage或 CachedNetworkImage .
关于image - 如何在 Flutter 中捕获 404 图像 URL 的 FetchFailure 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52637606/