Sub topic 12
Handling Loading and Error States
Assume you know basic Dart syntax, StatelessWidget, the build method, and how to run a Flutter project. This chapter focuses on images loaded from the network. Network requests can be slow, interrupted, or rejected, so an image widget needs a visible state for both progress and failure.
Showing progress with `loadingBuilder`
Image.network displays its image after Flutter downloads and decodes it. The loadingBuilder callback runs while that work is in progress.
The callback receives:
context, the widget's location in the widget tree.child, the image widget that will display the finished image.progress, anImageChunkEventwhile bytes are arriving, ornullwhen loading is complete.
Return child when progress is null. Otherwise, return a placeholder. This example keeps the image area at a fixed size so surrounding content does not jump as the image loads.
import 'package:flutter/material.dart';
class RemotePhoto extends StatelessWidget {
const RemotePhoto({super.key});
@override
Widget build(BuildContext context) {
return Image.network(
'https://picsum.photos/600/360',
width: 300,
height: 180,
fit: BoxFit.cover,
semanticLabel: 'A randomly selected landscape photo',
loadingBuilder: (context, child, progress) {
if (progress == null) {
return child;
}
final expected = progress.expectedTotalBytes;
final value = expected == null
? null
: progress.cumulativeBytesLoaded / expected;
return SizedBox(
width: 300,
height: 180,
child: Center(
child: CircularProgressIndicator(value: value),
),
);
},
errorBuilder: (context, error, stackTrace) {
return const SizedBox(
width: 300,
height: 180,
child: Center(
child: Icon(
Icons.broken_image_outlined,
size: 48,
semanticLabel: 'Photo could not be loaded',
),
),
);
},
);
}
}expectedTotalBytes can be null because some servers do not provide a content length. Passing null to CircularProgressIndicator.value creates an indeterminate spinner. When the total is known, the value shows determinate progress between 0.0 and 1.0.
Handling failures with `errorBuilder`
errorBuilder runs when the network request or image decoding fails. The error value describes the failure, and stackTrace can help during debugging. Do not display raw error details to users. Show a useful, understandable fallback instead.
An error can mean that the device is offline, the URL is wrong, the server returned an unsupported response, or the downloaded bytes were not a valid image. errorBuilder does not retry the request automatically. A retry action needs state that rebuilds the image, often by changing its key or URL.
The image state flow
For asset images, loading usually happens from the application's bundled files, but a missing asset or invalid file can still produce an error. Declare asset paths in pubspec.yaml, and test them on the platforms you support.
Exercise
Why should a loadingBuilder check whether progress is null before showing a progress indicator?
Show answer
A null progress value means the image has finished loading, so the callback should return the completed image instead of leaving the loading placeholder visible.