【问题标题】:Unable to load asset error while loading image in Flutter在 Flutter 中加载图像时无法加载资产错误
【发布时间】:2021-06-21 22:10:05
【问题描述】:

我为资产提供了两个空格,为图像位置提供了 4 个空格,但我也收到了错误。

这是我的pubspec.yaml

flutter:
  uses-material-design: true

  assets:
    - assets/port.jpeg

我收到错误消息

════════ Exception caught by image resource service ════════════════════════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: assets/port.jpeg

When the exception was thrown, this was the stack: 
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 236:49  throw_
packages/flutter/src/services/asset_bundle.dart 227:55                        load
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 45:50            <fn>
dart-sdk/lib/async/zone.dart 1362:47                                          _rootRunUnary
dart-sdk/lib/async/zone.dart 1265:19                                          runUnary
...
Image provider: AssetImage(bundle: null, name: "assets/port.jpeg")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#5a8a3(), name: "assets/port.jpeg", scale: 1)
════════════════════════════════════════════════════════════════════════════════════════════════════

这是我的目录

【问题讨论】:

  • 你能在你使用图像的地方显示代码吗?通常,在 pubspec.yaml 中的 assets 下,您只声明一个名为“images”的文件夹(您再次将其命名为“assets”),然后在代码中从该文件夹中输入您想要的图像。
  • 感谢您的回应

标签: flutter dart


【解决方案1】:

只需进行热重启。如果这不能解决您的问题,请卸载并在您的模拟器中重新运行您的应用程序,您会没事的

【讨论】:

    【解决方案2】:

    将你的资产写成:-

     assets:
       - assets/
    

    【讨论】:

      【解决方案3】:
      assets:
          
          - assets/port.jpeg
      

      另外,请确保图片的扩展名与目标图片扩展名匹配。

      【讨论】:

      • 感谢您的回复,在flutter clean并重新启动后它工作了
      猜你喜欢
      • 2021-01-17
      • 2019-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-28
      • 2021-05-10
      • 2020-04-11
      • 2020-04-26
      相关资源
      最近更新 更多