Sub topic 2
Declaring Files and Folders in `pubspec.yaml`
This chapter assumes that you know basic Dart syntax, can build a simple Flutter widget tree, and have edited pubspec.yaml before. You should also be comfortable with YAML indentation and running flutter pub get.
Flutter assets are files that ship with your application. An asset can be an image, JSON file, text file, sound, animation, or font. Flutter does not automatically include every file in your project. You must declare the files or folders that the app needs in the flutter section of pubspec.yaml.
How asset declarations work
The pubspec.yaml file is the configuration file for a Flutter project. Its flutter section contains an assets list. Each list item is a path relative to the project directory.
For example:
flutter:
assets:
- assets/images/logo.png
- assets/data/config.jsonThis configuration includes two individual files:
assets/images/logo.pngassets/data/config.json
The path is also the identifier that Flutter code uses later. If the declaration says assets/images/logo.png, code must use that exact path, including capitalization.
A folder can also be declared:
flutter:
assets:
- assets/images/The trailing slash matters. It tells Flutter to include the files in that directory. Use a folder declaration when a directory contains several related assets.
Declaring individual files and folders
Individual file declarations give you precise control over what enters the application package. They are useful when a folder contains files that should not ship, such as design drafts or unused images.
Folder declarations reduce maintenance when every file in a directory belongs in the app. Flutter includes the files in the declared directory and its relevant asset structure. Keep unrelated files outside that directory when possible.
A typical project might use this layout:
my_app/
assets/
data/
config.json
images/
logo.png
banner.png
lib/
main.dart
pubspec.yamlThe matching pubspec.yaml section is:
flutter:
assets:
- assets/data/config.json
- assets/images/The file path is case-sensitive on many build systems. A declaration for assets/images/logo.png does not reliably match a file named Logo.png.
You can declare several files and folders in one list:
flutter:
assets:
- assets/images/
- assets/data/config.json
- assets/data/help.txtDo not add a comma after a list item. YAML lists use a dash followed by a space.
Adding a declaration safely
When a Flutter project already has a flutter section, add assets inside that section instead of creating a second flutter key.
Before:
flutter:
uses-material-design: trueAfter:
flutter:
uses-material-design: true
assets:
- assets/images/
- assets/data/config.jsonAfter editing the file, run:
flutter pub getThis command reads the project configuration and refreshes package metadata. It is also a useful way to catch malformed YAML. A successful command does not prove that every path is correct, so check the spelling and location of each declared file as well.
The relationship between the project files and the declaration looks like this:
Registering custom fonts
Fonts are declared in the same flutter section, but they use a fonts list. Each family has a name and one or more font files.
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: italicHere, Raleway is the family name that Flutter code will use. The family name is a label chosen in the configuration. It does not have to match the file name, but using the font's normal family name avoids confusion.
The first file is the default regular font. The weight property maps a file to a numeric font weight. A value of 700 represents bold text. The style property marks the italic file.
A complete section can declare both assets and fonts:
flutter:
uses-material-design: true
assets:
- assets/images/
- assets/data/config.json
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 asset value must point to a real font file. Supported font formats depend on the target platform, but TrueType Font files with the .ttf extension are a common choice.
Common declaration errors
| Problem | Typical result | Correction |
|---|---|---|
| Wrong indentation | YAML parsing fails or the key is ignored | Align nested keys with spaces |
| Missing trailing slash on a folder | The folder is treated as a file path | Use `assets/images/` |
| Wrong capitalization | The asset cannot be found on some systems | Match the path exactly |
| Asset added but configuration not refreshed | Changes are not reflected immediately | Run `flutter pub get` and restart the app |
| Font family declared under the wrong key | The font is unavailable | Put `fonts` under `flutter` |
A declaration only makes a file available to Flutter. It does not display an image, parse JSON, or apply a font by itself. Later code must refer to the declared path or family name.
Exercise
Write the flutter section that includes every file in assets/images/ and registers assets/fonts/Roboto-Regular.ttf as the regular font in a family named Roboto.
Show answer
flutter: assets: - assets/images/ fonts: - family: Roboto fonts: - asset: assets/fonts/Roboto-Regular.ttf