skip to Main Content

My app uses path_provider’s getApplicationDocumentsDirectory to get the documents folder so I can save and edit some json files. That works fine on Android and iOS but I am struggling to get that to work on Chrome/Any Browser.

Is there an alternative?
This is how I use at the moment:

  static loadDirectory() async =>
      directory = await getApplicationDocumentsDirectory();

and then (for example):

  removeFile(String fileName) {
    File file = File("${directory.path}/$fileName.json");
    if (file.existsSync()) file.delete();
  }

What’s the approach for save/maintain files when dealing with browsers (I am considering to move my app to a website)

Thanks in advance

2

Answers


  1. @Icaroto On web, path_provider can’t directly access the file system like on Android and iOS due to security restrictions. For basic tasks like saving JSON files, opt for LocalStorage. For complex file operations, utilize web APIs for file handling.

    Login or Signup to reply.
  2. If you want to store a small amount of data you can use shared_preferences package. This package supports all platforms and you don’t need to deal with different platforms by yourself.

    final SharedPreferences prefs = await SharedPreferences.getInstance();
    
    await prefs.setString('data', json);
    final String? json = prefs.getString('data');
    

    https://pub.dev/packages/shared_preferences

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search