【问题标题】:Unable to load asset (file the app created)无法加载资产(应用程序创建的文件)
【发布时间】:2019-11-05 13:22:41
【问题描述】:

为什么会导致崩溃?

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: any,这是 getApplicationDocumentsDirectory() 所需要的。

【问题讨论】:

    标签: flutter


    【解决方案1】:

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

    final bytes = testFile.readAsBytesSync();
    

    rootBundle 包含与 构建时的应用程序。为 rootBundle 添加资源 您的应用程序,将它们添加到颤振的资产子部分 应用程序 pubspec.yaml 清单的部分。

    【讨论】:

    • 让我感到困惑的是它在热重载后工作。
    猜你喜欢
    • 2023-01-15
    • 1970-01-01
    • 2020-07-03
    • 2023-03-17
    • 2022-01-24
    • 2021-10-01
    • 1970-01-01
    • 2019-06-17
    • 2021-02-27
    相关资源
    最近更新 更多