无法加载资产(归档创建的应用)

时间:2019-11-05 13:22:41

标签: flutter

为什么这会导致崩溃?

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

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

我在pubspec.yaml中有一个依赖项path_provider: anygetApplicationDocumentsDirectory()需要它。

1 个答案:

答案 0 :(得分:1)

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

final bytes = testFile.readAsBytesSync();
  

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