flutter_gen icon indicating copy to clipboard operation
flutter_gen copied to clipboard

Feature: Preload assets

Open mrgnhnt96 opened this issue 2 years ago • 0 comments

As a developer, I would like my assets to load with as little delay as possible. This is achievable via precacheImage and precachePicture (using flutter_svg). It can be done with the current generated code. Adding the functionality to load all images or select would be ideal.

Current setup:

precacheImage(Assets.images.exampleImage, context);
precachePicture(Assets.icons.cseLogoWithUnderline.svg().pictureProvider, context);

Ideal Setup:

// Images
Assets.images.exampleImage.precache(context, ...);
Assets.images.precacheAll(context, ...);

// Icons
Assets.icons.cseLogoWithUnderline.precache(context, ...);
Assets.icons.precacheAll(context, ...);

// All
// ? not necessary, just throwing this out as an idea...
Assets.precacheAll(context, ...);

According to the docs, precaching does not require to use the same provider to successfully retrieve the cached asset.

// -- precachePicture (flutter_svg) --
/// The consumer of the image does not need to use the same
/// [PictureProvider] instance. The [PictureCache] will find the picture
/// as long as both pictures share the same key.

// -- precacheImage --
/// The consumer of the image does not need
/// to use the same [ImageProvider] instance. The [ImageCache] will find the image
/// as long as both images share the same key, and the image is held by the
/// cache.

mrgnhnt96 avatar Mar 22 '22 20:03 mrgnhnt96