Flutter:为什么Image.network无法加载图片。

3

我在没有网络的情况下启动了应用程序。 然后我使网络可用并点击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(() {});
              },
            ),
          ],
        ));
  }

https://github.com/flutter/flutter/pull/25980 - CopsOnRoad
我发现当图片加载失败时,该图片的URL将被认为是不可用的。再次请求相同的图片时将返回Null。有没有什么方法可以解决这个问题? - Jack Ma
1个回答

0

在你的Image.network(..)中没有需要改变的内容,因此它不会响应setState(),你的url和大小都是恒定的。

给它一些需要改变的东西,比如一个key。另外,由于它已经在缓存中存储了一个失败的图像,你可能还需要刷新缓存。这里有一个简单的示例:

Image.network(
  "https://picsum.photos/250?image=9",
  key: ValueKey(_key)
)

setState(() {
  imageCache.clear();
  _key = DateTime.now().millisecondsSinceEpoch.toString();
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接