Closed Nillouise closed 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.
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
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
💬 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 usegetFileFromCache
to get image cache and display the image, but thegetFileFromCache
returnFuture<FileInfo>
instead ofFileInfo
, so I must combine withFutureBuilder
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