Sub topic 10
Declaring assets and resolution variants
This chapter assumes you can write a StatelessWidget, you have a Flutter project that runs, and you have opened pubspec.yaml before. It covers how Flutter finds bundled image files, how you register them, and how the framework picks the right file for each screen density.
Why assets must be declared
An asset is a file that Flutter packages inside your app at build time. Images, fonts, JSON files, and sound clips can all be assets. Flutter does not scan your project folders for files. Anything you load with Image.asset must be listed in the flutter: section of pubspec.yaml.
If you forget to declare a file, the app builds and runs. The failure shows up at runtime, when the Image widget cannot find the file. That is one of the most common beginner errors, so it is worth learning the exact rules.
Declaring files and folders
You can list a single file, or a folder. A folder entry ends with a slash and includes the files directly inside it, not the files in its subfolders.
flutter:
uses-material-design: true
assets:
- assets/images/logo.png
- assets/images/backgrounds/
- assets/data/config.jsonHere logo.png and config.json are declared one by one. Every file directly inside assets/images/backgrounds/ is declared by the folder entry. A file in assets/images/backgrounds/dark/ would not be included. You would need to add that subfolder as its own line.
Some rules to remember:
- Paths are relative to the folder that contains
pubspec.yaml. - Indentation in YAML is meaningful. Use spaces, not tabs, and keep
assets:indented underflutter:. - The path in your Dart code must match the declared path exactly, including the
assets/prefix. - After editing
pubspec.yaml, runflutter pub getor save the file in your IDE. A hot reload alone does not always pick up new assets. A full restart is the safest step.
Loading a declared asset
import 'package:flutter/material.dart';
class Logo extends StatelessWidget {
const Logo({super.key});
@override
Widget build(BuildContext context) {
return Image.asset(
'assets/images/logo.png',
width: 120,
semanticLabel: 'Company logo',
);
}
}The string passed to Image.asset is the asset key. The key is the path you declared, written the same way.
Resolution variants
Phones have different pixel densities. The device pixel ratio tells you how many physical pixels make up one logical pixel. A ratio of 1.0 is a low-density screen, 2.0 is common on many phones, and 3.0 or more appears on high-end devices.
If you give the same 100 by 100 pixel image to every device, it looks sharp on a 1.0 screen and blurry on a 3.0 screen. Flutter solves this with resolution-aware assets. You supply the same image at several sizes, and the framework chooses the best one.
Folder layout
Put the base image in its folder. Then create sibling folders named after the scale factor, such as 2.0x and 3.0x, each holding a file with the same name.
assets/
images/
logo.png (1.0x, for example 100 x 100 pixels)
2.0x/
logo.png (200 x 200 pixels)
3.0x/
logo.png (300 x 300 pixels)The main file is the 1.0x version. The variants must have the same file name. Folders can use fractional ratios too, for example 1.5x.
Declaring variants
You declare only the main asset. Flutter finds the variants on its own.
flutter:
assets:
- assets/images/logo.pngYou write the code as if there were a single file:
Image.asset('assets/images/logo.png', width: 100, height: 100)On a device with a pixel ratio of 2.0, Flutter loads assets/images/2.0x/logo.png. The image is still displayed at 100 by 100 logical pixels, but it uses 200 by 200 physical pixels, so it stays sharp.
If you declare a folder with assets/images/, the variant subfolders are still picked up for the files inside that folder. You do not need to list 2.0x/ and 3.0x/ yourself.
How the best match is chosen
The diagram shows the lookup that happens when an Image.asset is built.
At build time, Flutter writes a manifest that lists each asset and its variants. At runtime, AssetImage reads the manifest and compares the device pixel ratio with each variant's scale. It picks the closest one. If the ratio falls between two variants, Flutter chooses the nearest, with ties going to the larger one, so a ratio of 2.5 would use the 3.0x file when no 2.5x file exists.
The scale also matters when you leave out width and height. A 200 by 200 pixel file in the 2.0x folder has a natural size of 100 by 100 logical pixels. A file in the main folder is treated as 1.0x, so a 200 by 200 pixel file there would appear as 200 by 200 logical pixels.
Missing variants
You do not need every variant. If only the main file exists, Flutter uses it on all devices. It will be scaled up on dense screens and may look soft. If a 3.0x file exists but a 2.0x file does not, a device with ratio 2.0 uses the closest available file.
Provide variants for images where sharpness matters, such as logos and illustrations. Photos that you display large often do not need them, and the extra files add to your app size.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Exception: "Unable to load asset" | The file is not listed in `pubspec.yaml`, or the path has a typo | Add the path, check spelling and case, then restart the app |
| Folder declared but a nested file is missing | Folder entries are not recursive | Declare each subfolder on its own line |
| Variant is never used | The variant folder name is wrong, or the file name differs from the main file | Use names like `2.0x` and keep file names identical |
| Image looks too large on dense screens | A high-resolution file was placed in the main folder | Move it into the matching `2.0x` or `3.0x` folder |
| YAML parse error | Tabs, or wrong indentation | Use spaces and align list items under `assets:` |
File names are case-sensitive on Android and on some other platforms. Logo.png and logo.png can work on your Mac or Windows machine and fail on a device.
Checking what Flutter sees
You can load an asset directly with rootBundle to confirm that it is bundled. This is useful when you debug a missing file.
import 'package:flutter/services.dart' show rootBundle;
Future<void> checkAsset() async {
try {
final data = await rootBundle.load('assets/images/logo.png');
print('Loaded ${data.lengthInBytes} bytes');
} catch (e) {
print('Asset not found: $e');
}
}If this call throws, the problem is in the declaration or the path, not in the Image widget.
Assets and performance
Declared assets are included in the app package, so every file adds to the download size. Declare only what you use. Keep source files that you do not ship, such as large design originals, outside the declared folders.
Resolution variants increase the package size, but each device decodes only the variant it needs. Memory use at display time is a separate topic, covered in the caching chapter, where cacheWidth and cacheHeight limit the decoded size.
Summary of the rules
- Declare each asset file, or its folder with a trailing slash, under
flutter: assets:. - Folder entries are not recursive.
- Put the 1.0x file in the main folder and add
2.0x,3.0xsibling folders with identical file names. - Declare and reference only the main path. Flutter finds the variants.
- Restart the app after changing
pubspec.yaml.
Exercise
Your project has assets/icons/home.png, assets/icons/2.0x/home.png, and assets/icons/3.0x/home.png. Write the pubspec.yaml assets entry and the Image.asset call that displays the icon at 48 by 48 logical pixels. Which file does a device with a pixel ratio of 3.0 load, and what pixel size should that file have?
Show answer
The entry is - assets/icons/ under flutter: assets: (or - assets/icons/home.png). The call is Image.asset('assets/icons/home.png', width: 48, height: 48). A device with ratio 3.0 loads assets/icons/3.0x/home.png, which should be 144 by 144 pixels (48 x 3).
Exercise
You add assets/images/banners/summer/hero.jpg and declare - assets/images/banners/ in pubspec.yaml. At runtime Flutter reports "Unable to load asset". Why, and how do you fix it?
Show answer
A folder declaration only includes files directly inside that folder, not files in its subfolders. Add - assets/images/banners/summer/ to the assets list, then restart the app.