ameiyil
ameiyil

Reputation: 411

NetworkImage cannot be assigned to type Widget

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'.

Upvotes: 17

Views: 12541

Answers (2)

ameiyil
ameiyil

Reputation: 411

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')
  ]
)

Upvotes: 24

R&#233;mi Rousselet
R&#233;mi Rousselet

Reputation: 277567

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.

Upvotes: 2

Related Questions