flutter-assets-pub-dev

Assets and Pub.dev Packages in Flutter

  • 3 min

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.json
Copied!

The 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.json
Copied!

We can also declare an entire folder:

flutter:
  assets:
    - assets/images/
Copied!

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,
)
Copied!

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')
Copied!

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.ttf
Copied!

Then we declare them in pubspec.yaml:

flutter:
  fonts:
    - family: Montserrat
      fonts:
        - asset: assets/fonts/Montserrat-Regular.ttf
        - asset: assets/fonts/Montserrat-Bold.ttf
          weight: 700
Copied!

And now we can use them from the application theme:

MaterialApp(
  theme: ThemeData(
    fontFamily: 'Montserrat',
  ),
  home: const HomeScreen(),
)
Copied!

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_name
Copied!

For example, to add http:

flutter pub add http
Copied!

This 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;
Copied!

How to choose packages

Don’t install the first package you see without looking. On Pub.dev, it’s good to review:

  1. Score and popularity, to detect abandoned packages.
  2. Date of the latest version, because Flutter changes quickly.
  3. Platform compatibility, especially if you need web or desktop.
  4. Documentation and examples, because a package without examples usually brings trouble on a Sunday afternoon.