Sub topic 9
Loading Images from Assets, Network, Files, and Memory
Assume you know Dart classes, Flutter widgets, the build method, and the pubspec.yaml file. This chapter focuses on choosing an image source. All four sources use the Image widget, but each source has different packaging, availability, and error behavior.
Choosing an image source
| Source | Typical use | Main requirement |
|---|---|---|
| Asset | Images shipped with the app | Declare the file in `pubspec.yaml` |
| Network | Images from a server or CDN | A reachable URL and network access |
| File | Images stored on the device | A valid local file path |
| Memory | Bytes received from another API or decoder | Image bytes in a `Uint8List` |
The Image widget displays an ImageProvider. Convenience constructors such as Image.asset and Image.network create the appropriate provider for you.
Assets packaged with the app
Use an asset when the image is known at build time and should work without a network connection. Register its directory in pubspec.yaml:
flutter:
uses-material-design: true
assets:
- assets/images/Then load the image by its path:
Image.asset(
'assets/images/logo.png',
width: 160,
height: 80,
fit: BoxFit.contain,
semanticLabel: 'Acme company logo',
)The path is relative to the project root. YAML indentation matters. After changing pubspec.yaml, run flutter pub get or restart the application if Flutter does not detect the change.
Images from the network
Use Image.network when the image is controlled by a server:
Image.network(
'https://example.com/photos/mountain.jpg',
width: 300,
height: 180,
fit: BoxFit.cover,
loadingBuilder: (context, child, progress) {
if (progress == null) return child;
return const SizedBox(
width: 300,
height: 180,
child: Center(child: CircularProgressIndicator()),
);
},
errorBuilder: (context, error, stackTrace) {
return const SizedBox(
width: 300,
height: 180,
child: Center(child: Icon(Icons.broken_image)),
);
},
)A network image can fail because of an unavailable server, an invalid URL, a timeout, or invalid image data. Always provide an error state for user-facing screens. loadingBuilder receives null progress after loading finishes.
> callout note > Do not assume that a successful HTTP response contains a valid image. The response can still fail during image decoding.
Files and memory
Image.file reads from a local file. It is useful for camera photos, downloads, and user-selected files. It requires dart:io, so it is not available in Flutter web applications.
import 'dart:io';
import 'package:flutter/material.dart';
Image.file(
File('/storage/emulated/0/Pictures/photo.jpg'),
width: 240,
height: 160,
fit: BoxFit.cover,
)Image.memory displays encoded image bytes, such as JPEG or PNG data returned by an API:
import 'dart:typed_data';
import 'package:flutter/material.dart';
Widget photoFromBytes(Uint8List bytes) {
return Image.memory(
bytes,
width: 240,
height: 160,
fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) =>
const Icon(Icons.broken_image),
);
}Use Image.file when you have a path. Use Image.memory when the bytes are already in memory. Do not convert between them unless another API requires that form.
Exercise
Which constructor should you use for a PNG bundled in your Flutter application, and which should you use for bytes returned by an API?
Show answer
Use Image.asset for the bundled PNG and Image.memory for the API bytes.