无法打开文件,路径='assets / responseFile'(操作系统错误:无此类文件或目录,errno = 2)

时间:2019-06-17 14:12:58

标签: flutter dart

我正在尝试写入存储在android Studio资产中的现有txt文件。但是,它会引发错误

assets:
  - assets/responseFile.txt

这是我的代码:

String content =  response.body.toString();
String path = "assets/responseFile.txt";
var file = new File(path);
file.writeAsString(content);

我也尝试过这种方式。但是没有运气

writingFile(String content) async {
  Directory appDocDir = await getApplicationDocumentsDirectory();
  String filePath = '${appDocDir.path}/assets/responseFile.txt';
  new File(filePath).writeAsString(content);}

1 个答案:

答案 0 :(得分:0)

您的pubspec.yaml错误。应该是:

flutter:
  assets:
    - assets/responseFile.txt #This in case the file is stored in the "assets" folder on the root of your Flutter project.

此外,您必须确保尊重标识。