ExactAssetImage class Null safety

Fetches an image from an AssetBundle, associating it with the given scale.

This implementation requires an explicit final assetName and scale on construction, and ignores the device pixel ratio and size in the configuration passed into resolve. For a resolution-aware variant that uses the configuration to pick an appropriate image based on the device pixel ratio and size, see AssetImage.

Fetching assets

When fetching an image provided by the app itself, use the assetName argument to name the asset to choose. For instance, consider a directory icons with an image heart.png. First, the pubspec.yaml of the project should specify its assets in the flutter section:

flutter:
  assets:
    - icons/heart.png

Then, to fetch the image and associate it with scale 1.5, use:

const ExactAssetImage('icons/heart.png', scale: 1.5)

Assets in packages

To fetch an asset from a package, the package argument must be provided. For instance, suppose the structure above is inside a package called my_icons. Then to fetch the image, use:

const ExactAssetImage('icons/heart.png', scale: 1.5, package: 'my_icons')

Assets used by the package itself should also be fetched using the package argument as above.

If the desired asset is specified in the pubspec.yaml of the package, it is bundled automatically with the app. In particular, assets used by the package itself must be specified in its pubspec.yaml.

A package can also choose to have assets in its 'lib/' folder that are not specified in its pubspec.yaml. In this case for those images to be bundled, the app has to specify which ones to include. For instance a package named fancy_backgrounds could have:

lib/backgrounds/background1.png
lib/backgrounds/background2.png
lib/backgrounds/background3.png

To include, say the first image, the pubspec.yaml of the app should specify it in the assets section:

  assets:
    - packages/fancy_backgrounds/backgrounds/background1.png

The lib/ is implied, so it should not be included in the asset path.

See also:

Inheritance
Annotations

Constructors

ExactAssetImage(String assetName, {double scale = 1.0, AssetBundle? bundle, String? package})
Creates an object that fetches the given image from an asset bundle.
const

Properties

assetName String
The name of the asset.
final
bundle AssetBundle?
The bundle from which the image will be obtained.
final
hashCode int
The hash code for this object.
read-onlyoverride
keyName String
The key to use to obtain the resource from the bundle. This is the argument passed to AssetBundle.load.
read-only
package String?
The name of the package from which the image is included. See the documentation for the ExactAssetImage class itself for details.
final
runtimeType Type
A representation of the runtime type of the object.
read-onlyinherited
scale double
The scale to place in the ImageInfo object of the image.
final

Methods

createStream(ImageConfiguration configuration) ImageStream
Called by resolve to create the ImageStream it returns.
protected">@protectedinherited
evict({ImageCache? cache, ImageConfiguration configuration = ImageConfiguration.empty}) Future<bool>
Evicts an entry from the image cache.
inherited
load(AssetBundleImageKey key, DecoderCallback decode) ImageStreamCompleter
Converts a key into an ImageStreamCompleter, and begins fetching the image.
inherited
loadBuffer(AssetBundleImageKey key, DecoderBufferCallback decode) ImageStreamCompleter
Converts a key into an ImageStreamCompleter, and begins fetching the image.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.
inherited
obtainCacheStatus({required ImageConfiguration configuration, ImageErrorListener? handleError}) Future<ImageCacheStatus?>
Returns the cache location for the key that this ImageProvider creates.
inherited
obtainKey(ImageConfiguration configuration) Future<AssetBundleImageKey>
Converts an ImageProvider's settings plus an ImageConfiguration to a key that describes the precise image to load.
override
resolve(ImageConfiguration configuration) ImageStream
Resolves this image provider using the given configuration, returning an ImageStream.
nonVirtual">@nonVirtualinherited
resolveStreamForKey(ImageConfiguration configuration, ImageStream stream, AssetBundleImageKey key, ImageErrorListener handleError) → void
Called by resolve with the key returned by obtainKey.
protected">@protectedinherited
toString() String
A string representation of this object.
override

Operators

operator ==(Object other) bool
The equality operator.
override