paulonia_cache_image 0.1.0

Flutter Android iOS web

Flutter package for cache images in storage or memory from the network or Google Cloud Storage. It supports Android, iOS and Web.

Paulonia Cache Image #

Flutter package for download and store images in the cache. It supports in-memory and storage cache in Android, iOS and Web for network and Google Cloud Storage images.

Gif

Usage #

To use this package add paulonia_cache_image as a dependency in your pubspec.yaml file.

You have to initialize the package in your main() function:

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  await PCacheImage.init();
  runApp(MyApp());
}

In the init() function you can initialize the default values of the Properties of ImageProvider. You can change a value in all Paulonia cache image widgets in your app.

Paulonia cache image extends ImageProvider, so you can use it with any widget that supports an ImageProvider only with the URL. By default, the image is cached in the platform storage:

Image(
  image: PCacheImage('https://i.imgur.com/jhRBVEp.jpg')
);
Image(
  image: PCacheImage(
    'gs://flutter-template-44674.appspot.com/images/user/0ooAw4dX5AeGhkH1JYkoWcdwvc72_big.jpg',
  )
);

In-memory cache #

With the default image cache in the platform storage there is a problem: when you make a setState() the package reads the storage and retrieve the image, this process causes the image to flicker!. With in-memory cache, this process is more quickly and there is no flicker. You can enable it as follows:

Image(
  image: PCacheImage('https://i.imgur.com/jhRBVEp.jpg', enableInMemory: true)
);

You can enable in-memory cache in all PCacheImage widgets in the init() function:

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  await PCacheImage.init(enableInMemory: true);
  runApp(MyApp());
}

Use only where your require. The problem with this approach is the memory usage increase. We recommend use only with images in widgets that run setState(), to erase the flicker.

CORS on web #

On the web when you try to make a request and download an image, it can throw an error with the CORS. Depends on the image type there is a solution:

Google Storage Images #

You must to enable CORS in your bucket

Network Images #

You can set a proxy in the init() function:

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  await PCacheImage.init(proxy: "https://cors-anywhere.herokuapp.com/");
  runApp(MyApp());
}

The proxy property is only used with network image in the way: "https://cors-anywhere.herokuapp.com/http://image.jpg".

Properties #

PCacheImage has the follow properties:

PropertyWhat does it doDefault
enableInMemoryEnable or disable the in-memory cachefalse
enableCacheEnable or disable the cachetrue
retryDurationIf the download fails, retry after this duration2s
maxRetryDurationMax accumulated time of retries10s
imageScaleThe image scale1.0

Author #

This packaged was made by ChrisChV and is used in all Paulonia projects.

5
likes
110
pub points
61%
popularity

Publisher

paulonia.dev

Flutter package for cache images in storage or memory from the network or Google Cloud Storage. It supports Android, iOS and Web.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

firebase, firebase_storage, flutter, hive, http, path_provider

More

Packages that depend on paulonia_cache_image