Baseflow / flutter_cache_manager

Generic cache manager for flutter
https://baseflow.com
MIT License
740 stars 429 forks source link

Can flutter_cache_manager return file cache synchronously? #271

Closed Nillouise closed 3 years ago

Nillouise commented 3 years ago

💬 Questions and Help

Currently I load image from Samba instead of url, so I use await DefaultCacheManager().putFile to put the downloaded image in local cache, and then use getFileFromCache to get image cache and display the image, but the getFileFromCache return Future<FileInfo> instead of FileInfo, so I must combine with FutureBuilder to display the image, which will cause the image flashing while switch APP page. I think the local cache file can be return synchronously (because the flutter Image.File() can be used synchronously).

I see this package have getFileFromMemory which can return synchronously, but I didn't found document about it.

I also post a thread on stackoverflow about this problem: https://stackoverflow.com/questions/65917811/how-to-display-a-futurefile-image

renefloor commented 3 years ago

The file is async because it needs to get information from the file storage. The getFileFromMemory still has a check whether or not the directory exist. I think we might be able te remove some checks here making that function synchronous if you are really sure that the file exists.

However, I think it is better to not put that function directly in a FutureBuilder, but for example in a bloc or using riverpod in a provider. That way the loading of the file is not coupled with the widget and not reloaded when a new widget is made.

Nillouise commented 3 years ago

Thank for your help, in fact, I am not sure the file exists because the local cache file may be deleted, but I will handle this situation using another placehold image, like FadeInImage. I aslo try to avoid using FutureBuilder because it is bad for this situation, I am now using a custom ImageProvider to display future, it work better now. Bloc and riverpod is new to me, it seem it can solve such problem, thank for your advice.

renefloor commented 3 years ago

This is a simple example to use a FutureProvider from riverpod, it is really easy: https://pub.dev/documentation/riverpod/latest/all/FutureProvider-class.html

This part Widget build(BuildContext, ScopedReader watch) is part of a consumerwidget: https://pub.dev/documentation/flutter_riverpod/latest/all/ConsumerWidget-class.html