Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

docs.flutter.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.

Learn more

Flutter 3.41 is live! Check out theFlutter 3.41 blog post!

ImageCache and ImageProvider changes

ImageCache requires implementers to override containsKey, and ImageProvider has marked resolve as @nonVirtual.

Important

Summary

#

ImageCache now has a method calledcontainsKey.ImageProvider subclasses should not overrideresolve, but instead should implement new methods onImageProvider. These changes were submitted as a single commit to the framework.

Description of change

#

The sections below describe the changes tocontainsKey andImageProvider.

containsKey change

#

Clients of theImageCache, such as a customImageProvider, may want to know if the cache is already tracking an image. Adding thecontainsKey method allows callers to discover this without calling a method likeputIfAbsent, which can trigger an undesired call toImageProvider.load.

The default implementation checks both pending and cached image buckets.

dart
boolcontainsKey(Objectkey){return_pendingImages[key]!=null||_cache[key]!=null;}

ImageProvider changes

#

TheImageProvider.resolve method does some complicated error handling work that should not normally be overridden. It also previously did work to load the image into the image cache, by way ofImageProvider.obtainKey andImageProvider.load. Subclasses had no opportunity to override this behavior without overridingresolve, and the ability to composeImageProviders is limited if multipleImageProviders expect to overrideresolve.

To solve this issue,resolve is now marked as non-virtual, and two new protected methods have been added:createStream() andresolveStreamForKey(). These methods allow subclasses to control most of the behavior ofresolve, without having to duplicate all the error handling work. It also allows subclasses that composeImageProviders to be more confident that there is only one public entrypoint to the various chained providers.

Migration guide

#

ImageCache change

#

Before migration, the code would not have an override ofcontainsKey.

Code after migration:

dart
classMyImageCacheimplementsImageCache{@overrideboolcontainsKey(Objectkey){// Check if your custom cache is tracking this key.}...}

ImageProvider change

#

Code before the migration:

dart
classMyImageProviderextendsImageProvider<Object>{@overrideImageStreamresolve(ImageConfigurationconfiguration){// create stream// set up error handling// interact with ImageCache// call obtainKey/load, etc.}...}

Code after the migration:

dart
classMyImageProviderextendsImageProvider<Object>{@overrideImageStreamcreateStream(ImageConfigurationconfiguration){// Return stream, or use super.createStream(),// which returns a new ImageStream.}@overridevoidresolveStreamForKey(ImageConfigurationconfiguration,ImageStreamstream,Objectkey,ImageErrorListenerhandleError,){// Interact with the cache, use the key, potentially call `load`,// and report any errors back through `handleError`.}...}

Timeline

#

Landed in version: 1.16.3
In stable release: 1.17

References

#

API documentation:

Relevant issues:

Relevant PRs:

Was this page's content helpful?

Unless stated otherwise, the documentation on this site reflects Flutter 3.38.6. Page last updated on 2025-10-28.View source orreport an issue.


[8]ページ先頭

©2009-2026 Movatter.jp