Search code examples
flutterflutter-dependencies

getApplicationDocumentsDirectory failing on Browsers (other possible approaches)


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


Solution

  • 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