'flutter download automatic image and save it in assets

in my app,I want to use SVG images from the asset file. but I want to check them in-the app, if the image asset is different from my image network from the server, I download the image network and replace it in my image asset and again read it from the asset to show them. how can I do that?

bool showSvg = false;

 void compareSvg(){
     if(value['svgimage']['name'] != Image.profileSvg){

     // how download svg network and replace it to assetfile

       }else{
      showSvg = true; });}

and its my code example :

 showSvg 
  ?SvgPicture.asset(Image.profileSvg) 
  :SvgPicture.asset(Image.newProfileSvg),


Solution 1:[1]

You can not compare images (technically you can, but it very complicated task) and replace assets file. You need to cache you image after first downloading, and set your asset as placeholder before your image are downloaded. Look in to dock here.

Edited:

Okay. I don't get your answer about svg, but if true here solution Add this pub for file caching to your project. And this code may help:

  File? _downloaded;

  void _fetchImage() async {
    const key = "profile";
    const url =
        "http://upload.wikimedia.org/wikipedia/commons/0/02/SVG_logo.svg";
    final file = await DefaultCacheManager().getSingleFile(url, key: key);

    setState(() {
      _downloaded = file;
    });
  }

  Widget _svgImageFetcher() {
    _fetchImage();

    Widget networkSvg;

    if (_downloaded == null) {
      networkSvg = SvgPicture.asset("assets/images/profile.svg");
    } else {
      networkSvg = SvgPicture.file(_downloaded!);
    }

    return Column(children: [Expanded(child: networkSvg)]);
  }

Just put _svgImageFetcher() Widget where you want to display image. And change default svg image and url.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1