dart Flutter:无法加载文本资源

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

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

  1. {
  2. "key": "value"
  3. }

我已经更新了我的pubspec.yaml

  1. assets:
  2. - assets/json/endpoints.json

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

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

它仍然给出错误

  1. ERROR: Unable to load asset: assets/json/endpoints.json
  2. package:flutter/src/services/asset_bundle.dart 221:7 PlatformAssetBundle.load
  3. ===== asynchronous gap ===========================
  4. dart:async _AsyncAwaitCompleter.completeError
  5. package:flutter/src/services/asset_bundle.dart PlatformAssetBundle.load
  6. ===== asynchronous gap ===========================
  7. dart:async _asyncThenWrapperHelper
  8. package:flutter/src/services/asset_bundle.dart PlatformAssetBundle.load
  9. 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路径前面
例如:

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

相关问题