Sub topic 1
What Counts as an Asset and How Flutter Bundles It
Prerequisites
This chapter assumes that you can read basic Dart, build a simple Flutter widget tree, edit pubspec.yaml, run flutter pub get, and follow YAML indentation rules. You should also know that Flutter apps run on several platforms, so a file path inside your project is not the same as a file path on the device.
An asset is a file that your application ships with and reads while it runs. Common assets include:
- Images such as PNG, JPEG, WebP, and SVG files
- Text files such as Markdown or plain text
- JSON configuration or translation files
- Audio and video files
- Custom font files such as TTF and OTF files
- Other binary files that your app needs
Flutter copies declared assets into the application bundle during a build. The bundle is the collection of files packaged for a target platform, such as Android, iOS, web, Windows, macOS, or Linux.
Your Dart code does not usually read these files through the operating system's normal file system. Instead, Flutter provides asset APIs that know how to find files inside the bundle.
Project Files and Bundled Files
An asset has two important paths:
- Its path in your Flutter project.
- Its logical asset key, which is the path your Dart code uses.
For example, suppose your project contains this file:
my_app/
assets/
images/
logo.pngThe project path is assets/images/logo.png. If you declare that path as an asset, the logical key used by Flutter is also assets/images/logo.png.
This key uses forward slashes, even when you develop on a platform that normally uses another path separator. Keep the spelling, capitalization, and extension exactly the same. Asset keys are commonly case-sensitive on Android, Linux, and web servers.
The asset is compiled into the application package. It is therefore available without a network connection, but it also contributes to the package's size.
What Flutter Bundles
Flutter bundles only files that are included by an asset declaration. A file can exist in your project and still be unavailable at runtime if it is not declared.
A declaration can name one file:
flutter:
assets:
- assets/data/config.jsonIt can also name a directory:
flutter:
assets:
- assets/images/A directory declaration includes the files directly inside that directory. It does not automatically include files in every nested directory. If your project uses nested directories, declare each directory that contains files you need.
For example:
assets/
images/
logo.png
icons/
menu.pngThe declaration assets/images/ includes logo.png, but you should also declare assets/images/icons/ when the application needs menu.png.
The flutter key must be at the correct indentation level. YAML uses spaces to show structure, so indentation is part of the configuration. Do not use tabs.
| Project file | Typical asset declaration | Typical runtime use |
|---|---|---|
| `assets/images/logo.png` | `- assets/images/logo.png` | `Image.asset(...)` |
| `assets/data/config.json` | `- assets/data/config.json` | `rootBundle.loadString(...)` |
| `assets/fonts/Raleway-Regular.ttf` | Declared under `fonts` | `TextStyle(fontFamily: 'Raleway')` |
The Asset Manifest
During the build, Flutter creates an asset manifest. A manifest is a generated list that maps logical asset keys to the files included in the application bundle.
The manifest allows Flutter to:
- Check which assets are available
- Locate an asset by its logical key
- Select a suitable image variant for the device
- Resolve assets that come from packages
You normally do not edit the manifest. Instead, edit pubspec.yaml and rebuild the app. Flutter regenerates the manifest from your declarations.
A hot reload is often not enough after adding or renaming an asset. Hot reload updates Dart code while the application is running, but a new asset declaration may require Flutter to rebuild the bundle. Run flutter pub get, then perform a full restart. If the asset still cannot be found, stop and run the application again.
Assets Are Application Data
Bundled assets are useful for data that should be present when the app starts. They are a good choice for:
- A default theme configuration
- Local translation data
- Help content
- Static illustrations
- Placeholder data used during development
They are not a good choice for data that changes frequently or must remain private. Users can often extract files from an installed application. Do not place passwords, private keys, or server secrets in an asset. Use a secure server or a platform-specific secret-storage mechanism for sensitive values.
A bundled asset is also read-only from the application's point of view. If a user edits a configuration file while using the app, that change should be written to an appropriate application data directory, not back into the bundled asset.
How Asset Choice Affects the Build
Every bundled file can increase the size of the application. A large video, a directory full of unused images, or several copies of the same data can make downloads and installation slower.
Keep declarations narrow when practical. Declare a specific file when only one file is needed. Use a directory declaration when the application genuinely uses most of that directory's contents. Remove obsolete files and declarations from the project.
The same source project can produce different platform bundles. Flutter may transform or compress some resources during a platform build, but you should still manage the original files carefully. Measure the final build when asset size matters.
A Minimal Example
The following declaration makes one image and one JSON file available:
flutter:
assets:
- assets/images/logo.png
- assets/data/config.jsonDart code can then refer to the logical keys:
import 'dart:convert';
import 'package:flutter/material.dart';
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>;
}
class AssetExample extends StatelessWidget {
const AssetExample({super.key});
@override
Widget build(BuildContext context) {
return Image.asset(
'assets/images/logo.png',
width: 120,
);
}
}The code does not use a device-specific path such as /storage/... or C:\.... Flutter resolves the logical key inside the application bundle.
Exercise
A project contains assets/data/help/intro.txt, but pubspec.yaml declares only assets/data/. What should you check if Flutter cannot load the text file?
Show answer
Check whether the nested directory is declared. A directory declaration does not automatically include files in nested directories, so add assets/data/help/ or declare the individual file, run flutter pub get, and perform a full restart.