Flutter-失败时默认映像为Image.network [英] Flutter - Default image to Image.network when it fails
本文介绍了Flutter-失败时默认映像为Image.network的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么方法可以控制Image.network()启动的异常,以便为它提供默认的AssetImage吗?
Is there any way you can control exceptions launched by Image.network() so you can provide it a default AssetImage ?
推荐答案
It depends on your use case, but one way to do it is to use FadeInImage
which has a property of img
for the image that is intended to load, and placeholder
, well, for the placeholder
FadeInImage(image: NetworkImage(url), placeholder: AssetImage(assetName)
您还可以收听直到加载图像为止,并亲自显示一个占位符,直到解析出图像为止.
You can also listen until the image is loaded and show a placeholder yourself until fetching the image resolves.
伪代码
bool _loaded = false;
var img = Image.network(src);
var placeholder = AssetImage(assetName)
@override
void initState() {
super.initState();
img.image.resolve(ImageConfiguration()).addListener((i, b) {
if (mounted) {
setState(() => _loaded = true);
}
});
}
@override
Widget build(BuildContext context) {
return YourWidget(
child: _loaded ? img : placeholder,
);
}
这篇关于Flutter-失败时默认映像为Image.network的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文