【问题标题】:unable to load asset [flutter]无法加载资产 [颤振]
【发布时间】:2019-12-01 22:16:48
【问题描述】:

我昨天尝试放一张图片,它工作得很好,但是当我再次尝试运行它时,它给了我这个错误:

Restarted application in ٢٬٧٦٥ms.

[38;5;248m════════ Exception caught by image resource service ════════════════════════════[39;49m
[38;5;244mThe following assertion was thrown resolving an image codec:[39;49m
Unable to load asset: cat.jpg

[38;5;244mWhen the exception was thrown, this was the stack[39;49m
[38;5;244m#0      PlatformAssetBundle.load[39;49m
[38;5;244m<asynchronous suspension>[39;49m
[38;5;244m#1      AssetBundleImageProvider._loadAsync[39;49m
[38;5;244m<asynchronous suspension>[39;49m
[38;5;244m#2      AssetBundleImageProvider.load[39;49m
[38;5;244m...[39;49m
[38;5;244mImage provider: AssetImage(bundle: null, name: "cat.jpg")[39;49m
[38;5;244mImage key: AssetBundleImageKey(bundle: PlatformAssetBundle#7533e(), name: "cat.jpg", scale: 1.0)[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m

这里是yaml:

flutter:
  uses-material-design: true
  assets:
    - images/cat.jpg

主要是

child: Image(
  image: AssetImage('cat.jpg'),
),

那么这里有什么问题?

【问题讨论】:

  • 抱歉,我现在这样做了

标签: android flutter dart visual-studio-code


【解决方案1】:

尝试更改此代码:

child: Image(
   image: AssetImage('cat.jpg'),
),

进入这个:

// add the full reference of the image
child: Image(
   image: AssetImage('images/cat.jpg'),
),

如果这对您不起作用,请尝试将您的 images 文件夹移动到 lib 文件夹中并更改您的引用,如下所示:

yaml 内部:

flutter:
   uses-material-design: true
   assets:
      - lib/images/cat.jpg

在你的飞镖代码中:

//the full reference of the image
child: Image(
   image: AssetImage('lib/images/cat.jpg'),
),

【讨论】:

    【解决方案2】:

    您必须像在 pubspec.yaml

    中声明的那样将 full path 提供给 Image Widget
    child: Image(
      image: AssetImage('images/cat.jpg'),
    ),
    

    附加您的 images 文件位于您的应用顶级目录中,如下所示

    ->(Your_App_Name)
      ->images
       ->cat.jpg
    

    【讨论】:

      【解决方案3】:

      在主文件中添加完整路径。

      Image(
         image: AssetImage('images/cat.jpg'),
      ),
      

      【讨论】:

        猜你喜欢
        • 2021-01-08
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        • 2022-08-10
        • 1970-01-01
        • 2020-12-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多