【问题标题】:Flutter image not displaying: "Unable to load asset"颤振图像未显示:“无法加载资产”
【发布时间】:2022-08-10 17:11:32
【问题描述】:

我没有在我的应用程序中看到我的图像,而是看到一个带有 X 的红色框,上面写着 Unable to load asset: images/aboutmaggie.png.

我创建了一个目录assets 和一个子目录images。目录assetspubspec.yaml 处于同一级别。

我将图像放入images 目录。当我在 Android Studio 中单击图像时,图像会显示。

pubspec.yaml 我有以下几行:

flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  assets:
    - assets/images/

我添加了

class AboutRoute extends StatelessWidget {
  const AboutRoute({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text(\'Kabbalistic Numerology\'),
        ),
        body: ListView(
            shrinkWrap: true,
            padding: const EdgeInsets.all(8),
            children: <Widget>[
              RichText(
                text: TextSpan(
                  children: <TextSpan>[
                    TextSpan(
                        text: \'About Maggie McPherson\',
                        style: TextStyle(
                          fontWeight: FontWeight.bold,
                          color: Colors.black,
                        )),
                  ],
                ),
                textAlign: TextAlign.center,
              ),
              RichText(
                text: TextSpan(
                  children: <TextSpan>[
                    TextSpan(
                        text:
                            \"Dr. Leslie Margaret Perrin McPherson...\",
                        style: TextStyle(
                          color: Colors.black,
                        )),
                  ],
                ),
              ),
              Image.asset(\'images/aboutmaggie.png\'), // <--this image doesn\'t load
            ]));
  }
}

我跑了flutter pub get。我运行了工具 > 颤振 > 颤振清洁。我关闭并重新启动了 Android Studio。

错误信息是:

======== Exception caught by image resource service ================================================
The following assertion was thrown resolving an image codec:
Unable to load asset: images/aboutmaggie.png

我尝试将另一个图像放入assets/images 并调用它,但第二个图像也不会加载。

这张图有什么问题?

    标签: flutter


    【解决方案1】:

    调用图像时出错,

    更改代码中的这一行

    Image.asset('images/aboutmaggie.png')
    

    到这条线

    Image.asset('assets/images/aboutmaggie.png')
    

    我希望这会有所帮助,谢谢

    【讨论】:

    • 我昨晚试过了,但没有用。我今天早上试过了,它奏效了。 Flutter(或 iOS 模拟器)是否缓存 pubspec.yaml?
    【解决方案2】:

    试试这样:

    Image.asset('assets/images/aboutmaggie.png'),
    

    【讨论】:

      【解决方案3】:

      试试下面的代码希望它对你有帮助。

      参考文档here添加资产和图像

      参考图像类here

      您的文件夹结构

      project directory
         - assets
             - images
                - your_image.png
      

      您的pubspec.yaml 文件

      flutter:
        assets:
          - assets/images/
      

      您的小部件:

         Image.asset(
              'assets/images/ln.png',//just change your image to my image
              height: 150,
              //width: 100,
            ),
      

      笔记- 如果您添加正确的代码和详细信息,那么您的问题没有解决然后停止(退出)项目/应用程序并重新启动

      结果屏幕->

      【讨论】:

      • 我昨晚试过了,但没有用。我今天早上试过了,它奏效了。
      【解决方案4】:

      您需要提供图像的完整路径。 代替 Image.asset('images/aboutmaggie.png')Image.asset('assets/images/aboutmaggie.png')

      【讨论】:

        【解决方案5】:

        这是缓存问题。昨晚我试过

        Image.asset('assets/images/aboutmaggie.png'),
        

        但这没有用。今天早上我启动了Android Studio,它问,

        pubspec.yaml has changed. Do you want to run flutter pub get?
        

        嗯,这很奇怪。我刚开始使用 Android Studio。我没有改变任何东西。我将路径改回'assets/images/aboutmaggie.png',运行flutter pub get,进行热重启并出现图像。

        然后我打开pubspec.yaml 并将其更改为

          assets:
            - assets/bowlingballs/
        

        我跑了flutter cleanflutter pub get,又热重启了一次,图像继续出现。

        我将路径改回'images/aboutmaggie.png',运行flutter pub get,进行了热重启,图像消失了。

        我的结论是,如果你改变这条路

        Image.asset('assets/images/aboutmaggie.png'),
        

        然后运行flutter pub get 并热启动然后将出现更改。但是,如果您对pubspec.yaml 进行更改,则在某些缓存被清除之前您不会看到更改。我昨晚遇到的问题是,当我设置pubspec.yaml 时,我在assets 之前有一个额外的空间。 pubspec.yaml 的那个版本被缓存了,pubspec.yaml 的后续版本被忽略了。 flutter clean 清除构建缓存,但 pubspec.yaml 显然不在构建缓存中。 pubspec.yaml 必须缓存在其他地方。

        【讨论】:

          【解决方案6】:

          在您的终端中,运行扑干净清除缓存,然后运行颤振酒吧得到颤振以重新获取您的资产文件夹

          这对我有用

          【讨论】:

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