NetworkToFileImage class

This is a mixture of FileImage and NetworkImage. It will download the image from the url once, save it locally in the file system, and then use it from there in the future.

In more detail:

Given a file and url of an image, it first tries to read it from the local file. It decodes the given File object as an image, associating it with the given scale.

However, if the image doesn't yet exist as a local file, it fetches the given URL from the network, associating it with the given scale, and then saves it to the local file. The image will be cached regardless of cache headers from the server.

Notes:

  • If the provided url is null or empty, NetworkToFileImage will default to FileImage. It will read the image from the local file, and won't try to download it from the network.

  • If the provided file is null, NetworkToFileImage will default to NetworkImage. It will download the image from the network, and won't save it locally.

  • If you make debug=true it will print to the console whether the image was read from the file or fetched from the network.

Tests

You can set mock files. Please see methods:

  • setMockFile(File file, Uint8List bytes)
  • setMockUrl(String url, Uint8List bytes)
  • clearMocks()
  • clearMockFiles()
  • clearMockUrls()

See also:

Inheritance

Constructors

NetworkToFileImage({File? file, String? url, double scale = 1.0, Map<String, String>? headers, bool debug = false})
const

Properties

debug bool
final
file File?
final
hashCode int
The hash code for this object.
no setteroverride
headers Map<String, String>?
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
scale double
final
url String?
final

Methods

createStream(ImageConfiguration configuration) ImageStream
Called by resolve to create the ImageStream it returns.
inherited
evict({ImageCache? cache, ImageConfiguration configuration = ImageConfiguration.empty}) Future<bool>
Evicts an entry from the image cache.
inherited
loadBuffer(NetworkToFileImage key, DecoderBufferCallback decode) ImageStreamCompleter
Converts a key into an ImageStreamCompleter, and begins fetching the image.
inherited
loadImage(NetworkToFileImage key, ImageDecoderCallback decode) ImageStreamCompleter
Converts a key into an ImageStreamCompleter, and begins fetching the image.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent 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<NetworkToFileImage>
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.
inherited
resolveStreamForKey(ImageConfiguration configuration, ImageStream stream, NetworkToFileImage key, ImageErrorListener handleError) → void
Called by resolve with the key returned by obtainKey.
inherited
saveImageToTheLocalFile(File file, Uint8List bytes) → void
toString() String
A string representation of this object.
override

Operators

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

Static Methods

clearMockFiles() → void
clearMocks() → void
clearMockUrls() → void
setMockFile(File file, Uint8List? bytes) → void
You can set mock files. It searches for an exact file.path (string comparison). To set an empty file, use null: setMockFile(File("photo.png"), null);
setMockUrl(String url, Uint8List bytes) → void
You can set mock urls. It searches for an exact url (string comparison).
startHttpOverride() → void
Call this if you want your mock urls to be visible for regular http requests.
stopHttpOverride() → void