Skip to content

Asset checks

The asset checks look at the files your package declares under flutter: assets: in its pubspec.yaml. assay has three. They report and never judge: all three are advisory, have no fix, and run in the command-line program only, so nothing of them appears in the editor or in dart analyze.

Check Reports
unused_assets A declared asset that no Dart source of the project names.
oversized_assets A declared image file larger than the size limit for its resolution.
missing_asset_variants A declared image, in a directory that uses resolution variants, that lacks a variant it is asked for, or variants without their base file.
Terminal window
assay assets # every check
assay assets unused_assets # one check
assay assets --reporter json # the result schema, with the assetChecks field
assay assets --reporter sarif:build/assets.sarif

On a package that declares assets/ and assets/icons/, with a 600 kB banner, an image nothing names, a logo with a 2.0x variant and a 2.0x star with no base file:

╭─ assay assets ───────────────────────────────────────────────────────────────╮
│ Preset recommended │
│ Checks unused_assets, oversized_assets, missing_asset_variants │
│ Files 4 │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Violations ────────────────────────────────────────────────── 4 in 4 files ─╮
│ Line Rule Tier │
│ ────────────────────────────────────── │
│ assets/banner.png 1 violation │
│ ! — oversized_assets advisory │
│ │
│ assets/icons/2.0x/star.png 1 violation │
│ ! — missing_asset_variants advisory │
│ The base file `assets/icons/star.png` of the 2.0x variant does not │
│ exist, and the image has no 3.0x variant. │
│ │
│ assets/icons/logo.png 1 violation │
│ ! — missing_asset_variants advisory │
│ The image has no 3.0x variant. │
│ │
│ assets/old.png 1 violation │
│ ! — unused_assets advisory │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Checks ─────────────────────────────────────────────────────────────────────╮
│ Check Tier Count │
│ ─────────────────────────────────────── │
│ oversized_assets advisory 1 │
│ The 1.0x image is 600 kB, over its limit of 500 kB. │
│ → Compress the image or store it in a smaller format; if it must stay this │
│ large, raise max_kb for its resolution or add its path to the check's │
│ exclude. │
│ │
│ missing_asset_variants advisory 2 │
│ → Add the files the image lacks: each missing variant in its resolution │
│ directory, and the base file, the 1.0x image by whose path the image is │
│ loaded; or add the image's path to the check's exclude if it is meant │
│ for fewer resolutions. │
│ │
│ unused_assets advisory 1 │
│ No Dart source of the project names the asset `assets/old.png`. │
│ → 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. │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ ✓ PASS ─────────────────────────────────────────────────────────────────────╮
│ 0 blocking · 4 advisory · 4 of 4 files │
╰──────────────────────────────────────────────────────────────────────────────╯
The asset inventory holds 4 assets, and the run took 0.9 s.
Reading 1 Dart file for what it names took 0.8 s.
  • assay assets exits with 0 whatever the checks find, 2 on a configuration or usage error, and 3 when it could not run (see assay assets).
  • An asset is a file as a whole: a result has no line and no column.
  • The checks stay silent in a package that declares no assets: no flutter: section, or no assets: list.
  • assay cross-file does not run them, and assay assets runs no cross-file check.

Each check has enabled and exclude under assay.assets, and some have keys of their own. <check>: false is the short form. All three are on under every preset.

assay:
assets:
unused_assets:
exclude: ["assets/native/**"]
oversized_assets:
max_kb: {3.0x: 2000}
missing_asset_variants:
variants: [2x]
Key Check Default What it is
max_kb oversized_assets {1.0x: 500, 2.0x: 1000, 3.0x: 1500} The size limit of an image by resolution, in kilobytes of 1,000 bytes.
variants missing_asset_variants 2.0x and 3.0x The variants an image is asked for.
image_extensions both png, jpg, jpeg, gif, webp, bmp, wbmp Which files are images, without regard to case.
  • A project’s max_kb is laid over the default, resolution by resolution: {3.0x: 2000} changes only 3.0x, and keeps 500 and 1000 for the others. A resolution can be added or changed, not removed. Every other value takes the place of its default whole.
  • Resolutions, in max_kb and variants, are written as Flutter names the directories: 2x and 2.0x are the same.
  • A suppression comment cannot silence an asset result: an asset has no line to write it on. Use the check’s exclude.
  • A misspelt name, or a key a check does not have, is a configuration error.
  • A change of any key is listed among the configuration changes at the gate, like every other key, although the checks’ results never enter it. assay config shows the asset checks where the project changed one, and --verbose always does.

See Configuring checks.

Never. Asset results are never in the gate and never in the baseline: not at turn end, not in the project judgement, not in baseline update --add. An agent is not told about them. Run them by hand, or as a report-only step in CI.

assay reads the pubspec at the project root as Flutter’s tool reads it when it bundles assets. Only the package at the project root is judged, not a nested example/ app or another member of a workspace.

  • An entry that names a file declares that file.
  • An entry that ends with / declares the files lying directly in that directory. A directory below it needs an entry of its own.
  • Files whose names start with a dot are left out, although Flutter’s tool bundles them, so no asset check judges a .gitkeep or a .DS_Store. The project-file check secret_in_shipped_file reads them, since a .env there ships with the app.
  • A map entry is read by its path; its flavors, platforms and transformers change nothing here.
  • 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 beside assets/logo.png. A resolution directory is one whose name ends with a number and x: 2x, 2.0x, 1.5x, icons_2x.
  • packages/<another package>/… entries, fonts:, shaders: and deferred-components: are never judged.

Each check’s page has the details.

  • unused_assets reads every Dart file of the project, the tests and generated files among them. An asset is named when a string constant names the path of its base file or of a variant, wherever it stands, or when the constant leading part of an interpolated or +-built text names its directory: 'assets/flags/$code.png' keeps alive every declared assets/flags/*.png, and not assets/flags/LICENSE.txt. A path built at run time with no constant directory part (Image.asset(path) with path from a server), native code, the web page and uses from other packages are not seen, so such an asset is reported: exempt it with the check’s exclude, as in the example under Configuration.
  • oversized_assets judges each image file on its own, by its size on disk: the base file as 1.0x, and each variant at its own resolution. A file exactly at its limit is not over it. A resolution that max_kb does not name takes the limit of the nearest lower one it names (1.5x takes 1.0x’s); one below them all takes the lowest. No image is decoded, and declared files that are not images (JSON, fonts, sounds) are not judged for size.
  • missing_asset_variants asks an image for its variants only where its directory already uses them: where one of its resolution directories holds an image. A project with no resolution directory gets no result at all. Only the variants of variants are asked for, and a variant counts only at its exact resolution: a 2.5x file does not stand in for 2.0x. A variant without its base is reported at any resolution. There is one result per image: at its base file, naming every variant it lacks, or, where the base file does not exist, at its lowest variant, naming the missing base.

Package types. In a package marked publish_to: none, unused_assets reports every declared asset without a reference. In a publishable package, which includes one with no publish_to line, it reports nothing and reads no Dart file, because the packages that depend on it can load its assets. oversized_assets and missing_asset_variants judge both package types.

Every reporter of check works. An asset result has no line, so the SARIF result has a region of startLine: 1 alone (GitHub code scanning requires a start line), a GitHub annotation has no line, and a GitLab entry stands on line 1, since that format asks every entry for a line. See assay assets.