【问题标题】:Error on retrieving assets for web (flutter)检索网络资产时出错(颤振)
【发布时间】:2020-01-04 09:05:38
【问题描述】:

我遇到了 Flutter WEB 的问题。我按照 Flutter 团队 (https://flutter.dev/docs/development/ui/assets-and-images) 的资产教程进行操作,但收到错误消息

 ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: assets/images/my_custom_image.png

When the exception was thrown, this was the stack:
dart:sdk_internal 4602:11                                  throw_
load
package:flutter_web/…/services/asset_bundle.dart:220
dart:sdk_internal 25309:9                                  <fn>
...

Image provider: AssetImage(bundle: null, name: "assets/images/my_custom_image.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#078a9(), name:
  "assets/images/my_custom_image.png", scale: 1)
════════════════════════════════════════════════════════════════════════════════════════════════════ 

我的 pubspecs.yaml

flutter:
  uses-material-design: true

  assets:
    - assets/images/

我正在加载这样的图像

Image.asset("assets/images/my_custom_image.png")

我在哪里搞砸了?

【问题讨论】:

    标签: image flutter assets flutter-web


    【解决方案1】:

    由于flutter for web 的状态仍为preview,因此资产处理与移动设备不同。assets 文件夹应位于web 文件夹中,并且图像的引用路径不应包含@987654325 @:

    Image.asset("images/my_custom_image.png")
    

    flutter for web 中,我们不需要引用 pubspec.yaml 中的资产。

    【讨论】:

    • 如果我将 assets 文件夹移动到 web 下,pubspec.yaml 会抱怨 The asset directory assets/images/ does not exist. Try creating the directory or fixing the path to the directory 如果我将文件夹保存在全局路径中,它不会抱怨
    • 搞定了。正如你所建议的,我从 yaml 中删除了所有内容并添加了路径。谢谢@Spatz!
    • 您将如何处理第三方包中的资产?
    • 在我的颤振项目中情况完全相反。 assets/images/some.jpg 在本地不起作用,但在远程它起作用。颤动的奇怪路径。
    猜你喜欢
    • 2021-11-24
    • 2020-04-15
    • 1970-01-01
    • 2020-09-02
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    相关资源
    最近更新 更多