gpt4 book ai didi

image - 如何在 Flutter 中捕获 404 图像 URL 的 FetchFailure 异常?

转载 作者:IT王子 更新时间:2023-10-29 07:00:02 25 4
gpt4 key购买 nike

我有这样的东西:

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 Exceptioncatch FetchFailurecatch (error)

我也尝试过根据错误代码返回一个小部件,但是 FetchStrategyBuilder 要我返回一个 bool 值。

最佳答案

您无法捕获 FetchFailure 错误的原因是因为它已经在 flutter_image 中捕获了.您收到的错误消息中指出了这一点:EXCEPTION CAUGHT BY PACKAGE:FLUTTER_IMAGE

您能否提供更多详细信息,说明您 try catch 异常的原因?如果这样做的原因是您试图显示占位符图像以防图像加载失败,您可以使用 FadeInImageCachedNetworkImage .

关于image - 如何在 Flutter 中捕获 404 图像 URL 的 FetchFailure 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52637606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com