Flutter: why Image.network cannot load image

Viewed 1651

I launch the app without network. Then I make network available and click setState button, the image is still not visible. Is there a solution?

  @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(() {});
              },
            ),
          ],
        ));
  }
1 Answers

There's nothing to change in your Image.network(..) so it's not going to respond to setState(), your url and sizes are all constant.

Give it something to change, like a key. Also, since it already has stored a failed image in its cache, you probably need to refresh the cache too. Here's a simple sample:

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

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