简体   繁体   English

如何在颤振应用程序的资产文件夹中写入和读取文件?

[英]How to write to and read from a file in my assets folder in a flutter app?

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)但是,这会导致以下错误: E/flutter ( 4944): FileSystemException: Cannot open file, path = 'colors.json' (OS Error: Read-only file system, errno = 30)

OS: Mac Catalina操作系统:Mac Catalina

pubspec entry:发布规范条目:

assets:
- assets/docs/colors.json

You can't write the files in the assets.您不能在资产中写入文件。 But you can use path_provider to save the modified file in app directory.但是您可以使用path_provider将修改后的文件保存在 app 目录中。

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.在构建过程中,Flutter 将资产放入一个特殊的归档文件中,该归档文件称为资产包,应用程序在运行时从中读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM