Sub topic 9
Fixing Missing Assets and Fonts That Do Not Render
This chapter assumes that you know basic Dart syntax, can build a simple Flutter widget tree, and can edit pubspec.yaml. You should also be comfortable running flutter pub get and reading YAML indentation.
Flutter bundles an asset only when the asset is declared correctly in pubspec.yaml. At runtime, your code must then use the same logical path that Flutter knows. A missing asset usually comes from a mismatch between those two steps.
Use a repeatable troubleshooting order
When an image, data file, or font does not work, check the problem in this order:
- Confirm that the file exists at the expected path.
- Check the
pubspec.yamldeclaration and its indentation. - Run
flutter pub get. - Stop and restart the application.
- Check the path used by Dart code.
- Read the first relevant error in the debug console.
- Check whether the asset belongs to the app or to a package.
A hot reload updates Dart code, but it does not always rebuild the asset bundle. After changing pubspec.yaml, use a full restart. If the problem remains, stop the app and run it again.
Check paths and YAML indentation
Suppose the project contains these files:
assets/images/logo.png
assets/data/config.jsonThe matching declaration is:
flutter:
assets:
- assets/images/
- assets/data/config.jsonThe flutter key must be at the top level. The assets key must be indented under flutter, and each entry must be indented under assets.
A directory entry includes files below that directory. A file entry includes only that file. These declarations are different:
flutter:
assets:
- assets/images/
- assets/data/config.jsonflutter:
assets:
- assets/images
- assets/dataThe first form is usually clearer because it shows which paths are directories and which path is a single file. Keep the slash on directory entries.
The path is case-sensitive on many build systems. Logo.png, logo.png, and LOGO.PNG can be different files. The path in code must match the declared path and the actual file name:
Image.asset('assets/images/logo.png')Do not use a path relative to the Dart file. Flutter asset paths are logical project paths, normally starting at the project root.
Interpret common asset errors
An error such as Unable to load asset means that Flutter could not find the requested logical asset in the built bundle. Check the spelling first, then the declaration.
An error mentioning an invalid YAML file usually means that indentation, punctuation, or a key is wrong. YAML uses spaces for structure. Avoid tabs.
An asset can also be declared in the wrong section. This declaration does not register the asset:
assets:
- assets/images/It must be nested under flutter:
flutter:
assets:
- assets/images/After correcting the file, run:
flutter pub get
flutter runIf the project still uses an old bundle, stop the running app and launch it again. Use flutter clean only when normal restart and dependency resolution do not fix a stale build. Cleaning removes generated build files, so the next build takes longer.
Diagnose text and JSON loading
rootBundle.loadString loads a text asset from the application bundle. It reports the same missing-asset problem as Image.asset when the path is wrong.
import 'dart:convert';
import 'package:flutter/services.dart' show rootBundle;
Future<Map<String, dynamic>> loadConfig() async {
final text = await rootBundle.loadString('assets/data/config.json');
return jsonDecode(text) as Map<String, dynamic>;
}If the asset loads but JSON decoding fails, the declaration is probably correct. Inspect the file contents instead. The file must contain valid JSON, use matching quotes, and avoid comments.
If the method is called during initState, handle its Future rather than assuming the result is immediately available. A file-loading failure is asynchronous, so it may appear after the widget has started building.
Fix fonts that do not render
A custom font needs a family name and one or more font files in pubspec.yaml:
flutter:
fonts:
- family: Raleway
fonts:
- asset: assets/fonts/Raleway-Regular.ttf
- asset: assets/fonts/Raleway-Bold.ttf
weight: 700
- asset: assets/fonts/Raleway-Italic.ttf
style: italicThe family name used in Dart is Raleway, not the file name:
Text(
'Custom text',
style: const TextStyle(
fontFamily: 'Raleway',
fontWeight: FontWeight.w700,
),
)If the font looks unchanged, check these details:
- The
fontFamilyvalue exactly matches the YAMLfamilyvalue. - The font files exist at the declared paths.
- The font file is a valid font and is not corrupted.
- The requested weight or style has a matching declaration.
- The app was fully restarted after changing
pubspec.yaml. - A more specific
TextStyleor theme is not overriding the font.
A font declared with weight: 700 matches FontWeight.w700. If you request FontWeight.w600 but declare only regular and bold files, Flutter may choose the closest available variant or synthesize a style. Declare the weights your design actually uses.
For app-wide use, set the family in the theme:
MaterialApp(
theme: ThemeData(fontFamily: 'Raleway'),
home: const Scaffold(
body: Center(child: Text('Hello, fonts')),
),
)Understand package asset paths
An asset inside a dependency package is not automatically treated as an app asset. For an image supplied by a package named my_icons, include the package name:
Image.asset(
'assets/logo.png',
package: 'my_icons',
)The package must declare that asset in its own pubspec.yaml. If the package documentation gives a different path, use that path exactly. A package font also needs to be declared by the package before your app can use its family name.
Follow the failure path
Keep a diagnostic checklist
| Symptom | First check | Likely correction |
|---|---|---|
| `Unable to load asset` | File name and declaration | Match the path and restart |
| Image loads as an error icon | Image format or bytes | Replace or repair the image |
| JSON loading throws | File contents | Make the JSON valid |
| Font looks unchanged | Family name and overrides | Use the declared family and restart |
| Bold text uses regular font | Weight mapping | Declare the requested weight |
Exercise
An image is stored at assets/images/logo.png, but Image.asset('assets/logo.png') fails. What should you check and change?
Show answer
The runtime path is missing the images directory. Declare assets/images/ under flutter.assets, run flutter pub get, and load the image with Image.asset('assets/images/logo.png'). Fully restart the app after changing the YAML file.