Sub topic 10
Declaring Assets and Resolution Variants in Flutter
Prerequisites
This chapter assumes that you know basic Dart syntax, can create Flutter widgets, and have a working Flutter project. You should also know that pubspec.yaml configures a Flutter project.
An asset is a file that your application packages with its code. Common assets include images, fonts, JSON files, and animations. Flutter cannot use an asset just because it exists in your project folder. You must declare it first.
Declaring an asset folder
Suppose your project contains this folder:
my_app/
assets/
images/
logo.png
mountain.png
lib/
main.dart
pubspec.yamlAdd the folder under the flutter: section of pubspec.yaml:
flutter:
uses-material-design: true
assets:
- assets/images/YAML uses indentation to show structure. Use spaces rather than tabs. The path is relative to the folder containing pubspec.yaml.
After saving the file, run flutter pub get. Flutter then includes the files in the application bundle. You can load one of them with Image.asset:
import 'package:flutter/material.dart';
class LogoView extends StatelessWidget {
const LogoView({super.key});
@override
Widget build(BuildContext context) {
return Image.asset(
'assets/images/logo.png',
semanticLabel: 'Company logo',
);
}
}The string passed to Image.asset must match the declared path and file name, including capitalization. A path such as assets/images/Logo.png does not necessarily refer to assets/images/logo.png.
You can declare one file instead of a whole folder:
flutter:
assets:
- assets/images/logo.pngDeclaring a folder includes files directly in that folder. It does not automatically include files in every nested folder, so declare nested folders when needed.
Resolution-aware image variants
A device has a device pixel ratio, or DPR. It describes how many physical pixels represent one logical Flutter pixel. A high-DPR screen can display more detail, but a large image also consumes more memory.
Flutter supports resolution variants for raster images. Place the variants in numbered subfolders:
assets/
images/
icon.png
1.5x/
icon.png
2.0x/
icon.png
3.0x/
icon.pngDeclare only the base folder:
flutter:
assets:
- assets/images/Use the base path in Dart:
Image.asset(
'assets/images/icon.png',
width: 48,
height: 48,
)Flutter treats icon.png as the main asset and searches for a suitable variant based on the device's DPR. The 2.0x/icon.png file should usually contain twice as many pixels in each dimension as the base file. Therefore, a 48 logical-pixel image might use a 96-by-96 pixel 2.0x file.
The application still refers to the base path. Do not select 2.0x/icon.png manually unless you have a specific reason to bypass Flutter's asset selection.
Common errors
- Placing
assets:outside theflutter:section. - Using tabs or incorrect YAML indentation.
- Declaring
assets/image/but loadingassets/images/logo.png. - Forgetting to run
flutter pub get. - Giving a variant a different file name from the base asset.
- Making every image extremely large, which increases download size and memory use.
Exercise
You have assets/photos/banner.png and a high-resolution version at assets/photos/2.0x/banner.png. What should you declare in pubspec.yaml, and what path should Image.asset use?
Show answer
Declare - assets/photos/ and use Image.asset('assets/photos/banner.png'). Flutter chooses the closest suitable resolution variant for the device.