Sub topic 3
Handling loading and error states
This chapter assumes you can write Dart classes, build a StatefulWidget or StatelessWidget, and run a Flutter project. It also assumes you know the Text, Image, and Icon widgets at a basic level.
Handling loading and error states
Image.network downloads bytes asynchronously, so the first frame may have no image yet. Two builders handle this. loadingBuilder runs while bytes arrive, and errorBuilder runs when the request or decoding fails.
import 'package:flutter/material.dart';
class NetworkPhoto extends StatelessWidget {
const NetworkPhoto({super.key});
@override
Widget build(BuildContext context) {
return Image.network(
'https://example.com/photo.jpg',
width: 200,
height: 120,
fit: BoxFit.cover,
loadingBuilder: (context, child, progress) {
if (progress == null) return child;
return const SizedBox(
width: 200,
height: 120,
child: Center(child: CircularProgressIndicator()),
);
},
errorBuilder: (context, error, stackTrace) =>
const Icon(Icons.broken_image),
);
}
}When progress is null, the image is complete and child is the final picture. Keep the placeholder the same size as the image so the layout does not jump.
Image caching and memory use
Flutter keeps decoded images in memory. Set cacheWidth to the display width so the engine decodes a smaller image. Use precacheImage to decode an image before it appears on screen.
import 'package:flutter/material.dart';
class HeroPhoto extends StatefulWidget {
const HeroPhoto({super.key});
@override
State<HeroPhoto> createState() => _HeroPhotoState();
}
class _HeroPhotoState extends State<HeroPhoto> {
@override
void didChangeDependencies() {
super.didChangeDependencies();
precacheImage(const AssetImage('assets/images/hero.png'), context);
}
@override
Widget build(BuildContext context) {
return Image.asset(
'assets/images/hero.png',
width: 300,
height: 180,
fit: BoxFit.cover,
cacheWidth: 600,
);
}
}Showing icons with the Icon widget
The Icon widget draws a glyph from Icons. Set semanticLabel so screen readers can announce it.
const Icon(
Icons.favorite,
color: Colors.red,
size: 32,
semanticLabel: 'Favorite',
)Styling icons with IconTheme
IconTheme sets the default color and size for every Icon below it.
const IconTheme(
data: IconThemeData(color: Colors.indigo, size: 28),
child: Row(
children: [
Icon(Icons.home),
Icon(Icons.search),
],
),
)Adding custom icons
Put the icon font in pubspec.yaml, then reference each glyph by its code point.
flutter:
fonts:
- family: MyIcons
fonts:
- asset: assets/fonts/MyIcons.ttfconst Icon(IconData(0xe900, fontFamily: 'MyIcons'), size: 24)The code point 0xe900 must match a glyph in your font file. Check the font's documentation for the correct value.
Making text, images, and icons accessible
Users can change their system font size, and Flutter scales Text to match. Avoid fixed heights for text containers. Give images and icons a label that describes their purpose.
| Widget | Property | Purpose |
|---|---|---|
| Image | `semanticLabel` | Describes the picture for screen readers |
| Icon | `semanticLabel` | Names the action or state |
| Text | Flexible height | Lets text grow with the user's font size |
Exercise
Exercise
An Image.network call has no errorBuilder. What does the user see if the URL returns a 404 response?
Show answer
Flutter throws an error and the image area stays empty, or the error appears in the debug console. Adding an errorBuilder that returns a fallback icon gives the user a visible, predictable state.