unused_assets
Reports a declared asset that no Dart source of the project names. Delete it, or exclude it if it is used in a way the check cannot see.
- Kind
- asset check (reports only)
- Tier
- advisory
- Host
- command-line program only (
assay assets) - Fix
- none
What it reports
Section titled “What it reports”A declared asset of the package that no Dart source of the project names: the image left behind when the screen that showed it was removed, the JSON file a feature no longer loads.
- Message: “No Dart source of the project names the asset
assets/images/old_banner.png.” - How to fix: “Delete the file and its variants if nothing needs them, or add the path to the check’s exclude when it is used in a way the check cannot see.”
These keep an asset alive, through its base file or any of its variants:
- a string constant whose value is the path, wherever it stands:
Image.asset,AssetImage,rootBundle.load, a class of paths, a test; - a text built from constants, or an interpolation whose constant leading part names a directory:
'assets/flags/$code.png'keepsassets/flags/de.pngandassets/flags/fr.png; - a generated accessor that hand-written code uses:
Assets.icons.home.image(); - the files below the constant
pathof anEasyLocalizationwidget.
These name no asset, so it is reported until you add its path to the check’s exclude:
- a path assembled at run time with no constant directory part:
Image.asset(path)withpathfrom a server or a configuration file,'$base/logo.png', a key looked up in the asset manifest; - a string constant that names only a directory, as in
manifest.listAssets().where((key) => key.startsWith('assets/flags/')): exempt the directory. The constantpathof anEasyLocalizationwidget is the exception; - a use from native code (Kotlin, Java, Swift, Objective-C) or the web page, and the URL the web build addresses an asset by,
assets/assets/..., as inImage.network('assets/assets/icon.png'); - a configuration file other than the pubspec, such as
flutter_launcher_icons.yamlorflutter_native_splash.yaml; - a use from another package, even of the same workspace: a run judges one analysis root.
An asset is reported once, at its base file, however many resolution variants it has.
Only a package marked publish_to: none is judged. In a publishable package, also one with no publish_to line, nothing is reported.
The asset checks only report: no result enters the gate or the baseline, and none fails a command.
Every declared asset is bundled into every build of the app, whether code loads it or not. An unused one costs every user download size, and it misleads the next reader, a person or an agent, who takes it for something in use. An agent that replaces a screen or an icon set rarely deletes what the old one used, and no single Dart file shows it.
The check is advisory because a project can load an asset by a path no Dart source names, such as from native code or by a path built at run time (see What it reports).
Example
Section titled “Example”With this pubspec, and assets/images/ holding logo.png and old_banner.png:
name: my_apppublish_to: none
flutter: assets: - assets/images/Image.asset('assets/images/logo.png')assets/images/old_banner.png is reported: no Dart source names it. Delete it, or add it to the check’s exclude if native code loads it.
Configuration
Section titled “Configuration”Under the assay: key of the project’s root analysis_options.yaml. The check is on under every preset.
| Key | Default | Effect |
|---|---|---|
assay.assets.unused_assets.enabled |
true |
false switches the check off. The short form unused_assets: false means the same. |
assay.assets.unused_assets.exclude |
[] |
Path globs where the check places no result. |
assay: assets: unused_assets: exclude: - assets/native/** # loaded by the Android and iOS code - assets/flags/** # addressed by a path built at run timeRunning it
Section titled “Running it”assay assets # every asset checkassay assets unused_assets # this one aloneThe check does not run in the editor, in assay check, in assay cross-file or in the gate. See assay assets.
Suppressing it
Section titled “Suppressing it”A suppression comment cannot silence the result: an asset has no line to write it on. Use the check’s exclude for an asset kept on purpose.
Details
Section titled “Details”All cases
The result
Section titled “The result”- The result is a violation with the source
assets, the ruleunused_assetsand the advisory tier. - The message adds ”, nor its variant” for an asset with one resolution variant, ”, nor any of its 2 variants” for more, and ”, which has no base file” for an asset with only variants. Such an asset is reported at its lowest-resolution variant.
- The result is of the whole file: it stands at line 1, column 1, and covers nothing. Its fingerprint is made of the check’s name, the asset’s path and an empty line text, so moving or renaming the asset makes a new result.
Which files are declared assets
Section titled “Which files are declared assets”The check reads the flutter: assets: list of the pubspec.yaml at the project root as Flutter’s tool reads it when it bundles assets:
- An entry that names a file declares that file. One that names nothing on disk declares nothing.
- An entry that ends with
/declares the files directly in that directory. A directory below it needs an entry of its own. A file whose name starts with a dot, such as.gitkeepor.DS_Store, is left out: Flutter’s tool bundles it, but assay does not report it. - A map entry is read by its
path. Itsflavors,platformsandtransformerschange nothing: an asset declared for one flavour is judged like any other. - Resolution variants. A file of the same name in a resolution directory beside a base file is a variant of that base, not an asset of its own:
assets/2.0x/logo.pngandassets/3.0x/logo.pngbesideassets/logo.png. A resolution directory is one whose name ends with a number followed byx:2x,2.0x,1.5x,3.x. The number need not start the name, soicons_2x/is one too. A variant whose base is not declared, or does not exist, is recorded as a variant without its base.
These declare nothing here, or are never judged:
- an entry
packages/<another package>/…, which declares an asset of that package; - an absolute path (
/etc/hosts), which Flutter’s tool refuses, or one that leads out of the package root (../shared/), so no result is placed outside the project; - a symbolic link that leads nowhere. A link that works is taken for the file it leads to, as Flutter’s tool bundles that file.
- the fonts under
flutter: fonts:, andshaders:anddeferred-components:.
A package with no pubspec.yaml, no flutter: section or no assets: list has no declared asset: the check reports nothing and reads no Dart file.
How a name of an asset is recognised
Section titled “How a name of an asset is recognised”The check reads every Dart file of the project: lib/, the tests, bin/, tool/, example/ and the rest, including files in assay.exclude and analyzer.exclude and generated files. Each is resolved as check resolves it, and a file that does not compile is read as far as the analyzer could make it out. It leaves out what check leaves out as no source of the project, such as .dart_tool or a directory directly under the project root whose name starts with a dot.
An asset is named, and not reported, when one of these names its base file or any of its variants:
- A string constant whose value is the path, wherever it stands: an argument of
Image.asset,AssetImageorrootBundle.load, a constant in a class of paths, a map of paths, a test. The check does not ask which call the string goes to. - The path through
packages/<the package's own name>/, as a package addresses its own assets:packages/my_app/assets/logo.pngnamesassets/logo.png. - A text put together from constants: adjacent strings, an interpolation of constant variables,
+of such texts.const dir = 'assets/sounds/'; const click = '${dir}click.mp3';namesassets/sounds/click.mp3. - A text built by interpolation or
+whose constant leading part names a directory (holds a/). It names every declared asset whose path starts with that leading part, holds its other constant parts in order, and ends with its trailing part.'assets/flags/$code.png'keeps aliveassets/flags/de.pngandassets/flags/fr.png, notassets/flags/LICENSE.txt. - A generated accessor that hand-written code uses. In generated code, a path counts only when code that is not generated refers to the declaration it lies in, directly or through other generated declarations. With an accessor class generated for every asset,
Assets.icons.home.image()keeps aliveassets/icons/home.pngand notassets/icons/settings.png, although the generated listvaluesnames both. A use ofAssets.icons.valueskeeps both alive. A file is generated when its name ends with.g.dart,.gen.dart,.freezed.dartor.mocks.dart, or when a comment before its first line of code says it is generated and not to be edited (// GENERATED CODE - DO NOT MODIFY BY HAND,// Generated file. Do not edit.). - A text the pubspec holds outside
flutter: assets:, such as the image path of a launcher-icon or splash-screen tool configured there. The entry underflutter: assets:itself is no name. - The translation files of easy_localization. An
EasyLocalizationwidget whosepathis a constant (path: 'assets/translations') names every file below that directory, since easy_localization loads them by locale name and noen.jsonappears in the code. Thepathis read asunused_l10nreads it, also through a subclass: apathpassed through: super(path: …), asuper.pathparameter, or a redirecting constructor (: this(path: …), a redirecting factory). The widget is recognised by the library that declares it, not by its name.
A path in a comment names nothing.
The two package types
Section titled “The two package types”| Package | Judged |
|---|---|
marked publish_to: none (an application, an internal package) |
every declared asset without a name is reported |
publishable: any other publish_to, or no publish_to line at all |
nothing is reported, and no Dart file is read: the packages that depend on it can load its assets, and they are not part of the project |
A package with no publish_to line counts as publishable, as pub treats it. Mark an application publish_to: none to have its assets judged; flutter create writes that line.
Only the package at the project root is judged, not a nested example/ app or a package of a pub workspace.
A pubspec.yaml that cannot be read as a YAML map naming the package (not valid YAML, as an edit in progress may leave it; empty; no name) leaves it unknown whether the package is published. The check then reports nothing, counts as not checked, and says so in one line in the box Not checked of the console report. A pubspec that is not valid YAML declares no asset either, so the other asset checks find nothing in that run.
Where no result is placed
Section titled “Where no result is placed”- At a file that
assay.excludeoranalyzer.excludecovers. - At a file that the check’s own
excludecovers.
The globs are read from the project root, like a rule’s exclude, and match the file the result would stand at: the base file, or the lowest variant where there is none.
Configuration in detail
Section titled “Configuration in detail”The short form switches the check off:
assay: assets: unused_assets: falseA name under assay.assets that is not an asset check is a configuration error that names the closest check (unused_asset → “Did you mean unused_assets?”). A key the check does not have is an error that names the keys it has. assay.cross_file.unused_assets is an unknown cross-file check: asset checks are configured under assay.assets only.
No fix
Section titled “No fix”No fix exists, and none is planned: a file is never deleted mechanically.