Question #334MediumWidgets & UIImportant

What is the difference between NetworkImage and Image.network in flutter?

#widget#image#networking

Answer

Overview

Both

text
NetworkImage
and
text
Image.network
display images from the internet, but they are different in type and usage.


Key Differences

Feature
text
NetworkImage
text
Image.network
Type
text
ImageProvider
text
Widget
UsageUsed in properties expecting
text
ImageProvider
Used directly in the widget tree
Renders itself❌ No — needs a widget to display it✅ Yes
Where to use
text
CircleAvatar
,
text
BoxDecoration
,
text
DecorationImage
Directly in widget tree

Image.network (Widget)

dart
// Use directly in widget tree
Image.network(
  'https://example.com/photo.jpg
  width: 200,
  height: 150,
  fit: BoxFit.cover,
  loadingBuilder: (context, child, progress) {
    if (progress == null) return child;
    return CircularProgressIndicator(
      value: progress.expectedTotalBytes != null
          ? progress.cumulativeBytesLoaded / progress.expectedTotalBytes!
          : null,
    );
  },
  errorBuilder: (context, error, stackTrace) {
    return Icon(Icons.broken_image);
  },
)

NetworkImage (ImageProvider)

dart
// Used in places that accept ImageProvider
CircleAvatar(
  backgroundImage: NetworkImage('https://example.com/avatar.jpg'),
  radius: 30,
)

// In Container decoration
Container(
  decoration: BoxDecoration(
    image: DecorationImage(
      image: NetworkImage('https://example.com/bg.jpg'),
      fit: BoxFit.cover,
    ),
  ),
)

When to Use Which

SituationUse
Displaying an image in the widget tree
text
Image.network
CircleAvatar with network image
text
NetworkImage
Container background from URL
text
NetworkImage
via
text
DecorationImage
Need loading/error callbacks
text
Image.network
Custom image widget properties
text
Image.network

Better Alternative: cached_network_image

dart
import 'package:cached_network_image/cached_network_image.dart';

CachedNetworkImage(
  imageUrl: 'https://example.com/photo.jpg
  placeholder: (context, url) => CircularProgressIndicator(),
  errorWidget: (context, url, error) => Icon(Icons.error),
)

// As ImageProvider
CachedNetworkImageProvider('https://example.com/avatar.jpg')

Best Practice: Use

text
cached_network_image
in production apps — it caches images locally so they don't re-download every time.