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

new AssetImage('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:

new AssetImage('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

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

See also:

Inheritance

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-only
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
scale double
The scale to place in the ImageInfo object of the image.
final
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

obtainKey(ImageConfiguration configuration) Future<AssetBundleImageKey>
Converts an ImageProvider's settings plus an ImageConfiguration to a key that describes the precise image to load. [...]
toString() String
load(AssetBundleImageKey key) ImageStreamCompleter
Converts a key into an ImageStreamCompleter, and begins fetching the image using loadAsync.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
resolve(ImageConfiguration configuration) ImageStream
Resolves this image provider using the given configuration, returning an ImageStream. [...]
inherited

Operators

operator ==(other) bool
The equality operator. [...]