2

I am trying to write to and read from a file that is in the assets folder in a flutter app. Generally, how should it be done? I tried the following approach:-

void writeToFile(String data) async {
  final String filename = 'colors.json';
  await rootBundle.load('assets/docs/colors.json').then((f){
    print("file loaded successfully.");
    File file = File(filename);
    file.writeAsString(data);
  });
}

However, this results in the following error: E/flutter ( 4944): FileSystemException: Cannot open file, path = 'colors.json' (OS Error: Read-only file system, errno = 30)

OS: Mac Catalina

pubspec entry:

assets:
- assets/docs/colors.json
iMujtaba8488
  • 241
  • 3
  • 9

1 Answers1

0

You can't write the files in the assets. But you can use path_provider to save the modified file in app directory.

Per documentation "Adding assets and images"

An asset is a file that is bundled and deployed with your app, and is accessible at runtime.

...

During a build, Flutter places assets into a special archive called the asset bundle that apps read from at runtime.

MαπμQμαπkγVπ.0
  • 5,887
  • 1
  • 27
  • 65