无法加载资产(归档创建的应用) [英] Unable to load asset (file the app created)

查看:45
本文介绍了无法加载资产(归档创建的应用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这会导致崩溃?

final directory = await getApplicationDocumentsDirectory();
final testFile = File("${directory.path}/test.txt");
await testFile.writeAsString("Hello there!", flush: true);
final ByteData bytes = await rootBundle.load(testFile.path);

rootBundle.load()调用原因:

未处理的异常:无法加载资产:/data/user/0/com.flutter.tests/app_flutter/test.txt

Unhandled Exception: Unable to load asset: /data/user/0/com.flutter.tests/app_flutter/test.txt

但是,如果我进行热重装,那么在我重新启动应用程序之前,它可以正常工作.

BUT if I do a hot reload then it works fine until I restart the app.

我在pubspec.yaml中有一个依赖项 path_provider:any ,这是 getApplicationDocumentsDirectory()所需要的.

I have a dependency path_provider: any in pubspec.yaml which is needed for the getApplicationDocumentsDirectory().

推荐答案

rootBundle 仅包含由pubspec.yaml指定并由您的应用程序构建的资产.它不应该访问在文件系统中创建的文件.使用文件或图像类打开创建的文件.

rootBundle only contains assets that were specified by pubspec.yaml and were built with your application. It's not supposed to access files created within file system. Use File or Image classes to open created files.

final bytes = testFile.readAsBytesSync();

rootBundle包含与构建时的应用程序.向rootBundle添加资源以用于您的应用程序,将它们添加到Flutter的Assets小节中应用程序的pubspec.yaml清单的一部分.

The rootBundle contains the resources that were packaged with the application when it was built. To add resources to the rootBundle for your application, add them to the assets subsection of the flutter section of your application's pubspec.yaml manifest.

这篇关于无法加载资产(归档创建的应用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆