How do I read and write image file locally for Flutter Web?

Viewed 2218

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

2 Answers

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

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...!

Related