Code Monkey home page Code Monkey logo

paulonia_cache_image's Introduction

Paulonia Cache Image

codecov License: MIT

Flutter package for download and store images in 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. To avoid an uncontrollable memory growth you can set the maximum number of images that to be stored in memory:

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

This functionality works as a queue, to save a new image to memory, the oldest one is deleted.

Clear Entire Cache

There could be situations where you might want to clear the whole cache may be to rebuild the cache again. Calling the following method will empty the cache.

await PCacheImage.clearAllCacheImages();

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:

Property What does it do Default
enableInMemory Enable or disable the in-memory cache false
enableCache Enable or disable the cache true
retryDuration If the download fails, retry after this duration 2s
maxRetryDuration Max accumulated time of retries 10s
imageScale The image scale 1.0
maxInMemoryImages Global variable, sets a max number of images in memory 7
clearCacheImage Deletes the image from cache false

Author

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

paulonia_cache_image's People

Contributors

ahmednfwela avatar chetan-cueclad avatar chrischv avatar juanleondev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

paulonia_cache_image's Issues

Devops

For deploy the first stable version, dev ops is needed

How much memory would a large list of images consume

Would it be idle to enable in-memory cache in the init() function.

In my use case, I have a large list that grows as one scrolls to a certain length.
The growing mechanism uses the Provider package but it still causes the flickering when ever I add item to the imageList with Provider. Although as per the docs

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.

No idea how setState is being called.

To keep it simple, would it be safe to enable in-memory caching for a large list of images?
Any feedback would be appreciated.

dependencies fail

Hi when to try dependencies fail

Because every version of paulonia_cache_image depends on http ^0.12.2 and oci_balance_app depends on http ^0.13.0, paulonia_cache_image is forbidden.
So, because oci_balance_app depends on paulonia_cache_image ^0.2.2, version solving failed.
pub get failed (1; So, because oci_balance_app depends on paulonia_cache_image ^0.2.2, version solving failed.)

Update dependencies

We need to update the dependencies so that the package can work on the new versions of flutter

Tests and coverage

For deploy the first stable version, tests and at least 75% of coverage is needed in the package

Flutter Web Image Stream issue

Screen Shot 2021-02-02 at 2 00 48 PM
Bad state: Stream has been disposed. An ImageStream is considered disposed once at least one listener has been added and subsequently all listeners have been removed and no handles are outstanding from the keepAlive method. To resolve this error, maintain at least one listener on the stream, or create an ImageStreamCompleterHandle from the keepAlive method, or create a new stream for the image.

This is the error I am getting on a flutter web project.
Can you please try to help on this?

Adding Cache time

it will be nice if we can define how much each image will be on cache

Functionality to add images to cache inMemory and keep them there

Context

Currently, the logic of InMemory cache is to save the image streams in a limited buffer (with a default size), and when the buffer is full and you need to save a new image, then the oldest image is deleted to save the new one.

New functionality

In some cases is needed to save an image in memory permanently and have control of when deleted.

Need a reliable Cache Refreshing mechanism.

Hi @ChrisChV This is by far one of the most efficient plugins to manage Firebase/Google storage images.

But we need a better and reliable cache Refreshing mechanism.

Consider the scenario : I have an image profile_pic.png which is cached on my app. Now if i replace this image directly in firebase/google storage by uploading a new version of this image from the Firebase/google Web UI - it doesn't rebuild the image cache on the app (i.e. - My App will still have the old version of the image).

To address this https://pub.dev/packages/firebase_image uses a very simple, efficient and cost effective mechanism. Basically to store metadata of the image and then comparing the metadata of the image as a Class B operation if enabled.

I strongly suggest we implement such mechanism as an optional parameter.

Flutter 3.0 Web Error

I received the following error when running my web app.

Flutter Web Bootstrap: Auto
ChromeProxyService: Failed to evaluate expression 'registerPlugins': InternalError: No frame with index 79.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.