dart Flutter:无法加载文本资源

y4ekin9u  于 2023-02-01  发布在  Flutter
关注(0)|答案(3)|浏览(164)

我的文件endpoints.json/WORKPLACE_DIR/assets/json/endpoints.json

{
  "key": "value"
}

我已经更新了我的pubspec.yaml

assets:
    - assets/json/endpoints.json

单位:/tests/some_test.dart
我知道

test('my test', () async {
      final endpoints = json.decode(await rootBundle.loadString("assets/json/endpoints.json"));
    }

它仍然给出错误

ERROR: Unable to load asset: assets/json/endpoints.json
package:flutter/src/services/asset_bundle.dart 221:7  PlatformAssetBundle.load
===== asynchronous gap ===========================
dart:async                                            _AsyncAwaitCompleter.completeError
package:flutter/src/services/asset_bundle.dart        PlatformAssetBundle.load
===== asynchronous gap ===========================
dart:async                                            _asyncThenWrapperHelper
package:flutter/src/services/asset_bundle.dart        PlatformAssetBundle.load
package:flutter/src/services/asset_bundle.dart 67:33  AssetBundle.loadString

我试过flutter clean,但它仍然给出这个错误。我不知道该怎么做。

ruarlubt

ruarlubt1#

我在图片上也遇到过类似的问题。原因很简单,我跳过了“assets:“之前的标签。如果单词“flutter:“和“assets:“在pubspec.yaml文件中处于同一级别,您将得到“无法加载资产”错误。因此,只需在“assets:“和“- assets/json/endpoints.json”之前添加标签。

zynd9foi

zynd9foi2#

您好,也许将资产添加到pubspec.yaml将有助于解决这个问题。

qeeaahzv

qeeaahzv3#

如果在子模块中,则需要将packages/your_submodule_name/放在assets路径前面
例如:

await rootBundle.loadString("packages/my_module/assets/json/endpoints.json");

相关问题