如何在本地为Flutter Web读写图像文件? [英] How do I read and write image file locally for Flutter Web?

查看:73
本文介绍了如何在本地为Flutter Web读写图像文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找将Flutter Web中的图像文件读写到本地目录中的方法.目的是使站点不再需要下载图像两次.例如:我们可以检查文件是否存在,如果不存在,则可以下载文件.

I was looking for ways to read and write image files in Flutter Web into local directory. The purpose is so the site won't need to download the images twice. For example: we can check if the file exist and if it doesn't then we can download it.

我们可以像这样在Flutter App中轻松地做到这一点:

We can do this easily in Flutter App like this:

  • 要访问本地存储中的文件,请执行以下操作:

  • To access the file in local storage:

// Getting App's local directory
final Directory localRootDirectory = await getApplicationDocumentsDirectory();

final String filePath = p.join(localRootDirectory.path, path, filename);
final tempFile = File(filePath);
return await tempFile.readAsBytes();

  • 然后我们保存新文件:

  • Then to save the new file we do:

    //Writing the image into file
    tempFile = await File(filePath).create(recursive: true);
    await tempFile.writeAsBytes(bytes);
    

  • 那么Flutter Web中是否有上述解决方案的等效方案?

    So is there any equivalent of the above solutions in Flutter Web?

    谢谢

    推荐答案

    我认为使用网络上的文件无法做到这一点.但是,您可以出于相同的目的使用 SharedPreferences .

    I don't think it can be done using files on web. However you can use SharedPreferences for the same purpose.

    SharedPreferences 支持Web: shared_preferences

    这篇关于如何在本地为Flutter Web读写图像文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆