![](/img/trans.png)
[英]How to read file from assets folder synchronously (without await) using flutter
[英]How to write to and read from a file in my assets folder in a flutter app?
我正在嘗試寫入和讀取顫動應用程序的資產文件夾中的文件。 一般應該怎么做? 我嘗試了以下方法: -
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);
});
}
但是,這會導致以下錯誤: E/flutter ( 4944): FileSystemException: Cannot open file, path = 'colors.json' (OS Error: Read-only file system, errno = 30)
操作系統:Mac Catalina
發布規范條目:
assets:
- assets/docs/colors.json
您不能在資產中寫入文件。 但是您可以使用path_provider將修改后的文件保存在 app 目錄中。
根據文檔“添加資產和圖像”
資產是與您的應用程序捆綁和部署的文件,可在運行時訪問。
...
在構建過程中,Flutter 將資產放入一個特殊的歸檔文件中,該歸檔文件稱為資產包,應用程序在運行時從中讀取。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.