扑面下载自动图像并将其保存在资产中

发布于 2025-01-22 17:18:45 字数 491 浏览 3 评论 0原文

在我的应用中,我想使用资产文件中的SVG图像。 但是我想在应用程序中检查它们,如果图像资产与服务器的图像网络不同,我下载了图像网络并将其替换为我的图像资产,然后再次从资产中读取它以显示它们。 我该怎么做?

bool showvg = false;

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

     // how download svg network and replace it to assetfile

       }else{
      showSvg = true; });}

及其我的代码示例:

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

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),

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舞袖。长 2025-01-29 17:18:45

您无法比较图像(从技术上讲可以,但任务非常复杂)并替换资产文件。首次下载后需要缓存图像,并在下载图像之前将资产设置为占位符。查看dock 在这里

编辑:

好的。我没有关于SVG的答案,但是如果在这里为真,则解决方案
添加此 pub 用于您项目的文件缓存。并且此代码可能会有所帮助:

  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)]);
  }

只需将要显示映像的位置放置_svgimagefetcher()窗口小部件即可。并更改默认的SVG图像和URL。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文