我在没有网络的情况下启动应用程序。 然后我使网络可用并单击 setState 按钮,图像仍然不可见。 有解决办法吗?
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
children: <Widget>[
Container(
color: Colors.blue,
child: Image.network(
'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3500755592,262843410&fm=26&gp=0.jpg'),
width: 100,
height: 100,
),
RaisedButton(
child: Text('setState'),
onPressed: () {
setState(() {});
},
),
],
));
}
最佳答案
您的 Image.network(..)
没有任何更改,因此它不会响应 setState()
,您的 url 和大小都是常量。
给它一些东西来改变,比如key
。此外,由于它已经在其缓存中存储了一张失败的图像,您可能还需要刷新缓存。这是一个简单的示例:
Image.network(
"https://picsum.photos/250?image=9",
key: ValueKey(_key)
)
setState(() {
imageCache.clear();
_key = DateTime.now().millisecondsSinceEpoch.toString();
});
关于Flutter:为什么 Image.network 无法加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54249481/