missing_asset_variants
Reports a declared image that lacks a resolution variant, in a directory that uses variants, or variants without their base file. Add the missing files.
- Kind
- asset check (reports only)
- Tier
- advisory
- Host
- command-line program only (
assay assets) - Fix
- none
What it reports
Section titled “What it reports”Two kinds of result, one per image, however many files it lacks:
- An image that lacks a variant it is asked for: a declared image, in a directory that uses variants, that has its base file but lacks one or more of the
variants(2.0x and 3.0x by default). The result is at the base file: “The image has no 3.0x variant.” - Variants without their base file: a declared image that has variants, at any resolution, and no base file. The result is at the lowest variant: “The base file
assets/icons/logo.pngof the 2.0x and 3.0x variants does not exist.”
A variant of assets/icons/logo.png is the file of the same name in a resolution directory of assets/icons/, such as assets/icons/2.0x/logo.png. Only images count (png, jpg, jpeg, gif, webp, bmp, wbmp by default); an SVG or a JSON file is never asked for variants.
Like every asset check, it reports and never judges: no result enters the gate or the baseline, and none fails a command.
A device picks the variant nearest to its pixel ratio, so a 3.0x phone scales a lone 1.0x image up three times, and it looks soft. An agent that adds an image to a directory with 2.0x/ and 3.0x/ usually adds the base file only, since nothing in a Dart file shows the variants. A variant without its base is worse: the path your code loads the image by is not there, and in a directory entry the image fails to load at run time.
The check is advisory because an image meant for one resolution (a full-screen background, a photograph) is often right as it is, and only you can say so.
Configuration
Section titled “Configuration”Under the assay: key of the project’s root analysis_options.yaml. The check is on under every preset unless you switch it off.
| Key | Default | Effect |
|---|---|---|
assay.assets.missing_asset_variants.enabled |
true |
false switches the check off. The short form missing_asset_variants: false means the same. |
assay.assets.missing_asset_variants.exclude |
[] |
Path globs where the check places no result. |
assay.assets.missing_asset_variants.variants |
[2.0x, 3.0x] |
The resolution variants an image is asked to have beside its base file. |
assay.assets.missing_asset_variants.image_extensions |
[png, jpg, jpeg, gif, webp, bmp, wbmp] |
The extensions of the files that are images, without the dot. |
assay: assets: missing_asset_variants: variants: [2x] # the project ships 1.0x and 2.0x only exclude: - assets/backgrounds/** # one resolution, on purposeExempt an image meant for one resolution with the check’s exclude: assets/backgrounds/**, or the path of the one image.
Running it
Section titled “Running it”assay assets runs every asset check, and assay assets missing_asset_variants runs this one alone. It reads no Dart file. See assay assets. The check does not run in the editor, in assay check, in assay cross-file or in the gate.
Suppressing it
Section titled “Suppressing it”A suppression comment cannot silence the result, since an asset has no line to write it on. Use the check’s exclude.
Details
Section titled “Details”All cases
The result
Section titled “The result”All four forms of the message:
- “The image has no 3.0x variant.”
- “The image has no 2.0x and no 3.0x variant.”
- “The base file
assets/icons/logo.pngof the 2.0x and 3.0x variants does not exist.” - “The base file
assets/odd.pngof the 1.5x variant does not exist, and the image has no 2.0x and no 3.0x variant.”
A variant without its base counts at any resolution, a 1.5x variant too. Where the image also lacks a variant it is asked for, the same message says so.
Both kinds carry the same how-to-fix line: “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.”
The result is a violation with the source assets, the rule missing_asset_variants and the advisory tier. It is of the file as a whole, at line 1, column 1, with no extent. Its fingerprint is the check’s name, the file’s path and an empty line text. So an image that gains one of the two variants it lacked keeps its fingerprint, with a shorter message. An image without its base whose base is then added, and that still lacks a variant, becomes a result of the first kind at the base file: a new fingerprint.
assay cross-file does not run it.
Why variants matter
Section titled “Why variants matter”Most projects ship one file per image and let Flutter scale it, which is why the check is silent where no directory uses variants. Where a directory has variants for most images and one is forgotten, that one stands out.
A variant without its base file:
- In a directory entry, Flutter’s tool does not bundle such a variant at all, so the image fails to load at run time.
- In a file entry, the tool bundles the variants, and a device whose resolution none of them is near takes the nearest one there is.
Either way the base file, the path your code loads the image by, is not there.
Which variants are asked for
Section titled “Which variants are asked for”The variants, by default [2.0x, 3.0x]:
- A variant outside the set, such as 1.5x or 4.0x, is neither asked for nor reported beside them. An image with its base, 1.5x, 2.0x and 3.0x is no result; one with its base, 1.5x and 4.0x lacks 2.0x and 3.0x.
- A variant counts at its exact resolution, as its directory name gives it: 2.5x is not 2.0x.
- The base file is 1.0x, so asking for
1.0xasks for nothing more than the base file.
A resolution directory is one whose name ends with a number followed by x, as Flutter’s tool recognises it: 2x, 2.0x, 1.5x, 3.x, and also icons_2x/, since the tool does not need the number to start the name. A directory uses variants when one of its resolution directories holds an image, of the same name or another. Where a directory holds images without variants on purpose beside others with them (icons with variants beside photographs without), exclude those images, or move them to a directory of their own.
Files whose names start with a dot (.gitkeep, .DS_Store) are left out. An empty resolution directory, or one with no image in it, is no use of variants.
Which images are declared is shared by every asset check: see Which files count as declared assets. An image declared by a file entry is asked for variants like one of a directory entry when its directory uses them, also when the variants there belong to images that are not declared.
Which files are images
Section titled “Which files are images”A declared file is an image when its extension is in image_extensions, compared without regard to case. The default is png, jpg, jpeg, gif, webp, bmp and wbmp, the same as for oversized_assets, each check with its own value. Any other file (an SVG, a JSON file, a video) is never asked for variants, and a resolution directory that holds only such files is no use of variants.
The check looks only at names and directory listings. No image is decoded, so it does not check that a 2.0x file really has twice the pixels of its base.
The two package types
Section titled “The two package types”Both are judged. A publishable package’s images are bundled into the apps that use it, so a missing variant there is missing in each of them.
Only the package at the project root is judged. An entry packages/<another package>/… declares another package’s asset, which is never judged.
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 be placed at: the base file, or the lowest variant where there is no base.
Configuration fine print
Section titled “Configuration fine print”variants is a list of resolutions, each written as Flutter names a resolution directory, like the keys of max_kb of oversized_assets. 2x and 2.0x are the same resolution, and assay config prints each as 2.0x, the lowest first. Your list replaces the default as a whole. An empty list asks for no variant, so the check reports only variants without their base file.
These are configuration errors, each reported with the file and the key. While the configuration is in error, no asset check runs:
- a
variantsthat is not a list, such asvariants: 2x; - an entry that is not a resolution:
2.0(a number, not a name),2.0X,x2,@2x,0x; - a resolution written twice, such as
2xand2.0x.
The message says what is expected: “The value has the wrong type: expected a list of resolutions, such as [2.0x, 3.0x].”
assay config prints the check where the project changed one of its keys, and assay config --verbose prints every key with the file its value came from. A change of any key is listed among the configuration changes, with the value before and after. [3.0x, 2x] is the default written another way, and is no change.
No fix
Section titled “No fix”There is no fix, and none is planned: a variant is an image someone has to draw or export.