Skip to content

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

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' keeps assets/flags/de.png and assets/flags/fr.png;
  • a generated accessor that hand-written code uses: Assets.icons.home.image();
  • the files below the constant path of an EasyLocalization widget.

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) with path from 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 constant path of an EasyLocalization widget 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 in Image.network('assets/assets/icon.png');
  • a configuration file other than the pubspec, such as flutter_launcher_icons.yaml or flutter_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).

With this pubspec, and assets/images/ holding logo.png and old_banner.png:

name: my_app
publish_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.

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 time
Terminal window
assay assets # every asset check
assay assets unused_assets # this one alone

The check does not run in the editor, in assay check, in assay cross-file or in the gate. See assay assets.

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.

All cases
  • The result is a violation with the source assets, the rule unused_assets and 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.

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 .gitkeep or .DS_Store, is left out: Flutter’s tool bundles it, but assay does not report it.
  • A map entry is read by its path. Its flavors, platforms and transformers change 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.png and assets/3.0x/logo.png beside assets/logo.png. A resolution directory is one whose name ends with a number followed by x: 2x, 2.0x, 1.5x, 3.x. The number need not start the name, so icons_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:, and shaders: and deferred-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.

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, AssetImage or rootBundle.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.png names assets/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'; names assets/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 alive assets/flags/de.png and assets/flags/fr.png, not assets/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 alive assets/icons/home.png and not assets/icons/settings.png, although the generated list values names both. A use of Assets.icons.values keeps both alive. A file is generated when its name ends with .g.dart, .gen.dart, .freezed.dart or .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 under flutter: assets: itself is no name.
  • The translation files of easy_localization. An EasyLocalization widget whose path is a constant (path: 'assets/translations') names every file below that directory, since easy_localization loads them by locale name and no en.json appears in the code. The path is read as unused_l10n reads it, also through a subclass: a path passed through : super(path: …), a super.path parameter, 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.

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.

  • At a file that assay.exclude or analyzer.exclude covers.
  • At a file that the check’s own exclude covers.

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.

The short form switches the check off:

assay:
assets:
unused_assets: false

A 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 exists, and none is planned: a file is never deleted mechanically.