2

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.

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

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

Thank you

Zenko
  • 2,319
  • 2
  • 20
  • 46

2 Answers2

1

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

SharedPreferences supports web: shared_preferences

Birju Vachhani
  • 6,072
  • 4
  • 21
  • 43
  • Thanks. But this does not store images. I'm using Firebase Storage and I don't want the client browser to re-download the same contents every time. Is that even possible? Isn't browsers has place to save images ? – Zenko Jun 08 '20 at 07:27
  • 1
    Browsers don't provide access to files due to some security reasons I think. It is true that shared_preferences do not store images but you can encode them into string and store it. then decode it when retrieving. Also, you can check if certain keys exists to check whether the image is cached or not... – Birju Vachhani Jun 08 '20 at 10:25
0

Although the answer above is the accepted answer, but the solution above but only works while the browser is open.

Finally I found out that I do not need to worry about saving it locally anyway the browser keeps temporary internet files in its cache. Duh...!

Zenko
  • 2,319
  • 2
  • 20
  • 46