Sub topic 8
Using Assets and Fonts That Ship in Packages
This chapter assumes that you know basic Dart syntax, can build a simple Flutter widget tree, and can edit pubspec.yaml. You should also know YAML indentation rules and have used flutter pub get.
A Flutter package can include reusable assets such as images, icons, JSON files, and fonts. Your application can load these files without copying them into its own assets directory. This keeps shared resources with the package that owns them.
Declaring Package Assets
A package declares its assets in its own pubspec.yaml. The package must list each file or directory under its flutter section.
For example, a package named my_icons might contain this structure:
my_icons/
pubspec.yaml
assets/
logo.png
icons/
search.pngIts pubspec.yaml can declare both the logo and the icon directory:
name: my_icons
flutter:
assets:
- assets/logo.png
- assets/icons/An application then adds the package as a dependency. A published package uses a version:
dependencies:
flutter:
sdk: flutter
my_icons: ^1.0.0A local package can use a path dependency instead:
dependencies:
my_icons:
path: ../my_iconsRun flutter pub get after changing dependencies. Flutter can then find the package and its declared resources.
Loading an Image from a Package
Use Image.asset for an image that belongs to a package. The asset value is relative to the package root, and the package argument identifies the package:
import 'package:flutter/material.dart';
Widget packageLogo() {
return Image.asset(
'assets/logo.png',
package: 'my_icons',
width: 120,
height: 120,
);
}The path is assets/logo.png, not my_icons/assets/logo.png. Flutter combines the path with the package name internally.
You can use the same pattern with other asset widgets, such as AssetImage:
const logoProvider = AssetImage(
'assets/logo.png',
package: 'my_icons',
);This distinction matters because an asset path without package means “look in the application.” An asset path with package means “look in this dependency.”
Loading Package Data Files
Package assets are not limited to images. You can read a package's text or JSON file with rootBundle. Pass the same package name to loadString:
import 'dart:convert';
import 'package:flutter/services.dart' show rootBundle;
Future<Map<String, dynamic>> loadPackageConfig() async {
final text = await rootBundle.loadString(
'packages/my_icons/assets/data/config.json',
);
return jsonDecode(text) as Map<String, dynamic>;
}Notice the path difference. When calling rootBundle.loadString, include the packages/ prefix:
packages/<package-name>/<asset-path>For an image widget, provide the package separately with package: 'my_icons'. For a direct bundle lookup, use the full package path.
The package must declare the file:
flutter:
assets:
- assets/data/config.jsonA missing declaration causes the lookup to fail even when the file exists in the package directory.
Using Fonts from a Package
A package can register a font family in its own pubspec.yaml. Each font file should have a matching weight or style declaration:
flutter:
fonts:
- family: PackageSans
fonts:
- asset: assets/fonts/PackageSans-Regular.ttf
- asset: assets/fonts/PackageSans-Bold.ttf
weight: 700
- asset: assets/fonts/PackageSans-Italic.ttf
style: italicThe application can use the font in a TextStyle. Set both fontFamily and package:
import 'package:flutter/material.dart';
class PackageHeading extends StatelessWidget {
const PackageHeading({super.key});
@override
Widget build(BuildContext context) {
return const Text(
'Shared design system',
style: TextStyle(
fontFamily: 'PackageSans',
package: 'my_icons',
fontWeight: FontWeight.w700,
),
);
}
}The package field tells Flutter where to find the font family. Without it, Flutter may search the application's registered fonts instead.
You can apply a package font to an entire subtree with DefaultTextStyle:
DefaultTextStyle(
style: const TextStyle(
fontFamily: 'PackageSans',
package: 'my_icons',
),
child: const Text('Text from a shared package'),
)For a package font used throughout the application, configure the app theme:
MaterialApp(
theme: ThemeData(
textTheme: const TextTheme(
bodyMedium: TextStyle(
fontFamily: 'PackageSans',
package: 'my_icons',
),
),
),
home: const Scaffold(
body: Center(child: Text('Package font')),
),
)How Package Paths Are Resolved
The two common APIs use different-looking forms:
| Resource | API | Package reference |
|---|---|---|
| Image | `Image.asset` | `package: 'my_icons'` |
| JSON or text | `rootBundle.loadString` | `packages/my_icons/...` |
| Font | `TextStyle` | `package: 'my_icons'` |
Flutter still uses the package's declared asset path in each case. The API determines how you provide the package identity.
Common Errors
Check these items when a package resource does not load:
- The dependency name matches the value in
package. - The package's
pubspec.yamldeclares the file or directory. - The path uses forward slashes and matches capitalization exactly.
- You ran
flutter pub getafter changing the dependency. Image.assetuses the package argument, not a package-prefixed path.rootBundleusespackages/package_name/before the package-relative path.- A package font's family name matches its
fontsdeclaration. - A package font's
TextStyleincludes the correctpackagevalue. - The font weight and italic style match the registered variant.
Exercise
An image is declared in the shared_ui package at assets/icons/menu.png. What call loads it with Image.asset?
Show answer
Use Image.asset('assets/icons/menu.png', package: 'shared_ui'). Do not add shared_ui/ to the asset path.