简体   繁体   English

Flutter 图像未显示:“无法加载资产”

[英]Flutter image not displaying: "Unable to load asset"

Instead of seeing my image in my app I'm seeing a red box with an X that says Unable to load asset: images/aboutmaggie.png.我没有在我的应用程序中看到我的图像,而是看到一个带有 X 的红色框,上面写着Unable to load asset: images/aboutmaggie.png.

I made a directory assets with a subdirectory images .我制作了一个带有子目录images的目录assets The directory assets is at the same level as pubspec.yaml .目录assetspubspec.yaml处于同一级别。

I put the image into the images directory.我将图像放入images目录。 When I click on the image in Android Studio the image displays.当我在 Android Studio 中单击图像时,图像会显示。

在此处输入图像描述

In pubspec.yaml I have these lines: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/

I added我添加了

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
            ]));
  }
}

I ran flutter pub get .我跑了flutter pub get I ran Tools > Flutter > Flutter Clean.我运行工具 > Flutter > Flutter 清洁。 I shut down and restarted Android Studio.我关闭并重新启动了 Android Studio。

The error message is:错误信息是:

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

I tried putting another image into assets/images and calling that but the second image wouldn't load either.我尝试将另一个图像放入assets/images并调用它,但第二个图像也不会加载。

What's wrong with this picture?这张照片有什么问题?

Try like this:试试这样:

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

there is an error in calling the image,调用图像时出错,

change this line in your code更改代码中的这一行

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

to this line到这条线

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

I hope this helps, thankyou我希望这会有所帮助,谢谢

Try below code hope its helpful to you.试试下面的代码希望它对你有帮助。

Refer documation here for Adding assets and images请参阅此处的文档以添加资产和图像

Refer Image Class here在此处参考图像 Class

Your folder structure您的文件夹结构

project directory
   - assets
       - images
          - your_image.png

Your pubspec.yaml file您的pubspec.yaml文件

flutter:
  assets:
    - assets/images/

Your Widget:您的小部件:

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

Note - If you adding correct code and details then your problem not resolved then stop(exit) the project/app and relaunch again注意- 如果您添加了正确的代码和详细信息,那么您的问题未解决然后停止(退出)项目/应用程序并重新启动

Result Screen->结果屏幕-> 在此处输入图像描述

It was cacheing issue.这是缓存问题。 Last night I tried昨晚我试过

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

but that didn't work.但这没有用。 This morning I started up Android Studio and it asked,今天早上我启动了 Android Studio,它问,

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

Well, that's odd.嗯,这很奇怪。 I'd just started Android Studio.我刚刚开始 Android Studio。 I hadn't changed anything.我没有改变任何东西。 I changed the path back to 'assets/images/aboutmaggie.png' , ran flutter pub get , did a hot restart and the image appeared.我将路径改回'assets/images/aboutmaggie.png' ,运行flutter pub get ,进行热重启并出现图像。

Then I opened pubspec.yaml and changed it to然后我打开pubspec.yaml并将其更改为

  assets:
    - assets/bowlingballs/

I ran flutter clean , flutter pub get , did another hot restart, and the image continued to appear.我跑了flutter cleanflutter pub get ,又热启动了,图像继续出现。

I changed the path back to 'images/aboutmaggie.png' , ran flutter pub get , did a hot restart and the image was gone.我将路径改回'images/aboutmaggie.png' ,运行flutter pub get ,进行热重启,图像消失了。

My conclusion is that if you change this path我的结论是,如果你改变这条路

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

then run flutter pub get and a hot restart then the change will appear.然后运行flutter pub get并热重启,然后将出现更改。 But if you make changes to pubspec.yaml you won't see changes until some cache is cleared.但是,如果您对pubspec.yaml进行更改,则在清除某些缓存之前您不会看到更改。 The problem I had last night was that when I set up pubspec.yaml I had an extra space before assets .我昨晚遇到的问题是,当我设置pubspec.yaml时,我在assets之前有一个额外的空间。 That version of pubspec.yaml got cached and subsequent versions of pubspec.yaml were ignored.该版本的pubspec.yaml被缓存,后续版本的pubspec.yaml被忽略。 flutter clean clears the Build cache but pubspec.yaml apparently isn't in the Build cache. flutter clean清除构建缓存,但pubspec.yaml显然不在构建缓存中。 pubspec.yaml must be cached somewhere else. pubspec.yaml必须缓存在其他地方。

In your terminal, run flutter clean to clear up the cache, then run flutter pub get for flutter to fetch your assets folder afresh在您的终端中,运行flutter clean以清除缓存,然后运行flutter pub get for flutter 重新获取您的资产文件夹

This is what worked for me这对我有用

You need to provide complete path of the image.您需要提供图像的完整路径。 Replace Image.asset('images/aboutmaggie.png') with Image.asset('assets/images/aboutmaggie.png') .Image.asset('images/aboutmaggie.png')替换为Image.asset('assets/images/aboutmaggie.png')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM