Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in NetworkImage and Image.network?

Tags:

flutter

dart

Are there any difference in both of them?

Whats the disadvantage and which one is easier to use for normal situation?

like image 319
Daniel Mana Avatar asked Sep 09 '18 09:09

Daniel Mana


People also ask

How do I display list of images in flutter?

and append the image. Step 4: Now in the body of the scaffold, Create a ListView widget, In ListView first, we will use the row to show the item count and a button. Now add one more child to the listview, which will show the list of images.


Video Answer


1 Answers

Are there any difference in both of them?

Yes. They are different.

  • NetworkImage class creates an object the provides an image from the src URL passed to it. It is not a widget and does not output an image to the screen.
  • Image.network creates a widget that displays an image on the screen. It is just a named constructor on the Image class(a stateful widget). It sets the image property using the NetworkImage . This image property is used finally to display the image.

    class Image extends StatefulWidget{   Image(...){}; //default Constructor     //the argument src is passed to the NetworkImage and assinged to the image property   Image.network(String src, {...}) : image = NetworkImage(src, ...);     final ImageProvider image;    @override   Widget build(BuildContext context){     display the image     return RawImage(image: image,       ...     );   } } 

Whats the disadvantage and which one is easier to use for normal situation?

There is no disadvantage. You should use the one the suits the need. For example consider:

  1. CircleAvatar widget that displays acircle that represents a user takes backgroundImage. It requires an ImageProvider. So you pass NetworkImage(http://image.com)
  2. FadeInImage that displays a placeholder while the original image is loading also takes a ImageProvider for its image property. So you can provide it NetworkImage(http://image.com).

If you just want to display the image as a widget on screen use Image.network and use NetworkImage wherever an ImageProvider is expected.

like image 70
Vamsi Krishna Avatar answered Oct 12 '22 02:10

Vamsi Krishna