Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NetworkImage cannot be assigned to type Widget

Tags:

flutter

I tried to create add an image to my ListView by doing the following

new ListView(
  children: <Widget>[
    new NetworkImage('my_image_url')
  ]
)

and got the following error:

The element type 'NetworkImage' can't be assigned to the list type 'Widget'.

like image 435
ameiyil Avatar asked Nov 07 '17 00:11

ameiyil


2 Answers

NetworkImage isn't a Widget, instead it:

Fetches the given URL from the network, associating it with the given scale.

Thus, it's used in Widgets like CircleAvatar to provide the source for its image.

The correct way to add an image via a url is to use Image.network('url'):

new ListView(
  children: <Widget>[
    new Image.network('my_image_url')
  ]
)
like image 156
ameiyil Avatar answered Sep 27 '22 16:09

ameiyil


NetworkImage doesn't just:

Fetches the given URL from the network, associating it with the given scale.

It can be used to display an image. In fact you will need it for something called DecoratedBox, which IS a Widget.

Compared to the simple Image.network('my_url'), using DecoratedBox has a few advantages. Because you can alter that image. You have access to filters, slice, shadows, or even borders.

like image 33
Rémi Rousselet Avatar answered Sep 27 '22 16:09

Rémi Rousselet