precached_network_image 1.3.0 copy "precached_network_image: ^1.3.0" to clipboard
precached_network_image: ^1.3.0 copied to clipboard

Flutter library to load and cache network images to disk and support to precache images to memory in advance to avoid the flash screen caused by the delay time.

Precached network image #

pub packageStar on Github

Flutter library to load and cache network images to disk and support to precache images to memory in advance to avoid the flash screen caused by the delay time.

Features #

  • Multi-platform support: Works on iOS, Android, and Web platforms
  • Advanced caching: Dual-layer caching with memory and disk storage
  • Precaching: Load images to memory in advance to prevent flickering
  • Web compatibility: Uses localStorage for web platform caching
  • Flexible storage: Automatic platform-specific storage selection
  • Error handling: Comprehensive error handling and fallback support
  • Performance optimized: Efficient image loading and caching strategies

Platform Support #

Platform Cache Storage Memory Precaching Status
iOS File System ✅ Yes ✅ Supported
Android File System ✅ Yes ✅ Supported
Web localStorage ✅ Yes ✅ Supported

Usage #

The PrecachedNetworkImage can be used directly.

PrecachedNetworkImage(
  url: 'https://picsum.photos/200',
  width: 200, 
  height: 200,
  precache: true, // default is false, true for next time loading from memory in advance.
  fit: BoxFit.fill,
  placeholder: (context, url) => const Icon(Icons.person),
  errorWidget: (context, url, error) {
    log("get image failed code: $error");
    return const Icon(Icons.error);
  },
),

When you want to precache images to memory to avoid the delay time, you can call PrecachedNetworkImageManager's precacheNetworkImages() which read the disk's files to memory like on the launch widget in advance.

@override
void initState() {
  super.initState();
  PrecachedNetworkImageManager.instance.precacheNetworkImages(isLog: true);
}

PrecachedNetworkImageManager handle about precache(memory) and cache(disk) all methods:

/// precache all urls(or the target param urls) to files in memory
/// usage:
///   you can call this method in advance(eg. on launch) to avoid the flash screen caused by the delay time
///   after set the parameter [precache] of PrecachedNetworkImage(..., precache: true) to true.
precacheNetworkImages({List<String> urls}); 

/// add the url which you want to precache next time
addPrecache({@required String url});

/// delete the url which you cancel to precache 
deletePrecache({@required String url});

/// delete the file of given url cache in memory and disk
deleteImageCache({@required String url});

/// clean all precache's files of given urls cache in memory and disk
cleanImageCache();

/// clean all urls precache
cleanPrecache();

/// clean all urls precache and file cache in memory and disk
cleanCaches();

Installation #

Add this to your package's pubspec.yaml file:

dependencies:
  precached_network_image: ^1.2.6

Requirements #

  • Dart SDK: >=2.17.0 <4.0.0
  • Flutter: >=1.17.0

Author #

Visit my github: janlionly
Contact with me by email: janlionly@gmail.com

Contribute #

I would love you to contribute to PrecachedNetworkImage

License #

PrecachedNetworkImage is available under the MIT license. See the LICENSE file for more info.

7
likes
150
points
787
downloads

Publisher

verified publisherjanlionly.dev

Weekly Downloads

Flutter library to load and cache network images to disk and support to precache images to memory in advance to avoid the flash screen caused by the delay time.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

crypto, dev_colorized_log, flutter, http, path, path_provider, shared_preferences

More

Packages that depend on precached_network_image