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

问题描述

为什么这会导致崩溃?

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()调用原因:

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

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();

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

09-19 00:47