我试图覆盖app_name/assets/file.csv路径中CSV文件的内容。我在这些操作中使用提供程序和csv。
下面是应该写入现有CSV文件的代码:
updateQuantity(customer, index, qty, list) async {
list[index][2] = qty;
String updatedCsv = const ListToCsvConverter().convert(list);
print(updatedCsv);
final dir = await getApplicationDocumentsDirectory();
String path = "${dir.path}/assets/$customer.csv";
File file = await File(path);
file.writeAsString(updatedCsv);
}我收到的错误:
E/flutter ( 4352): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: FileSystemException: Cannot open file, path = '/data/user/0/com.example.data_collection_2/app_flutter/assets/Test.csv' (OS Error: No such file or directory, errno = 2)我已经在网上搜索过了,但似乎没有任何东西能帮助我写到文件中。
提前感谢
发布于 2022-08-08 10:55:07
您确定目录assets已经存在了吗?如果CSV不存在,那么它将失败,因为目录assets可能也不存在。
解决这个问题的方法是执行File file = await File(path).create(recursive: true);。
这将创建文件和目录assets,如果它们还不存在。如果它们确实存在,它将什么也不做,也不会失败,因此无论是哪种方式,它都是安全的。
https://stackoverflow.com/questions/73275871
复制相似问题