Assets are files that we bundle alongside our application to use at runtime.
In Flutter, we typically refer to images, icons, fonts, JSON files, or any static resource we want to include inside the App. They don’t come from the internet, they don’t depend on an API, and they don’t appear out of thin air: you have to declare them.
What is an asset
An asset is a file that lives inside the project and that Flutter copies when building the application.
For example, we can have this structure:
assets/
images/
logo.png
avatar.png
data/
config.jsonThe file is there, yes. But Flutter won’t include it automatically. First, you have to register it in pubspec.yaml.
Declaring assets in pubspec.yaml
The pubspec.yaml file is where Flutter looks for project dependencies and resources. To add assets, we use the flutter section.
flutter:
assets:
- assets/images/logo.png
- assets/images/avatar.png
- assets/data/config.jsonWe can also declare an entire folder:
flutter:
assets:
- assets/images/Watch out for indentation in YAML. Two spaces too many or too few and Flutter will get angry. And when YAML gets angry, it usually doesn’t explain things kindly.
Using images in the interface
Once the image is declared, we can use it with Image.asset.
Image.asset(
'assets/images/logo.png',
width: 120,
)This loads the image from the application package, without making any network requests.
If the image comes from the internet, then we use Image.network:
Image.network('https://example.com/avatar.png')The difference is important. Image.asset is for local resources and Image.network is for remote resources.
Adding custom fonts
Fonts are also assets. First, we place them in a folder, for example:
assets/
fonts/
Montserrat-Regular.ttf
Montserrat-Bold.ttfThen we declare them in pubspec.yaml:
flutter:
fonts:
- family: Montserrat
fonts:
- asset: assets/fonts/Montserrat-Regular.ttf
- asset: assets/fonts/Montserrat-Bold.ttf
weight: 700And now we can use them from the application theme:
MaterialApp(
theme: ThemeData(
fontFamily: 'Montserrat',
),
home: const HomeScreen(),
)The Pub.dev repository
Pub.dev is the official package repository for Dart and Flutter.
If you want to use HTTP, persistence, icons, maps, sensors, or almost any other function, it’s normal to look for a package there. Pub.dev displays the documentation, score, compatibility, and activity of the package.
Adding a package
The most convenient way is to use the terminal:
flutter pub add package_nameFor example, to add http:
flutter pub add httpThis modifies the pubspec.yaml and adds the dependency with a compatible version.
Then we import it in Dart:
import 'package:http/http.dart' as http;How to choose packages
Don’t install the first package you see without looking. On Pub.dev, it’s good to review:
- Score and popularity, to detect abandoned packages.
- Date of the latest version, because Flutter changes quickly.
- Platform compatibility, especially if you need web or desktop.
- Documentation and examples, because a package without examples usually brings trouble on a Sunday afternoon.