How to download image from network and use as an asset image offline in flutter

Issue

I am using sqlflite flutter package to manage a small database for my app, there is a table where I have some urls for few images from the internet , but I need to save them and use the files instead of urls to show this images later. I mean I want to do something like this :

bool internet
internet ? Image.network('the_url') : Image.assets("path to image in my assets folder")

so, is there any way to fetch images from urls and save it to be able to access it later?

Solution

You can download the image with NetworkAssetBundle and convert to Unint8List

final ByteData imageData = await NetworkAssetBundle(Uri.parse("YOUR_URL")).load("");
final Uint8List bytes = imageData.buffer.asUint8List();

Then you can load it through Image.memory() widget

Image.memory(bytes);

You can store that data in sqflite and retrive when needed

Answered By – Guru Prasad mohapatra

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published