简体   繁体   English

无法在颤动中加载资产(图像)

[英]Unable to load asset (image) in flutter

This is how the code (app) looks like.这就是代码(应用程序)的样子。

Aight people this is my first time doing this so I'll try to be precise and neat... Aight 人这是我第一次这样做,所以我会尽量精确和整洁......

So I've just started learning flutter and I am watching a course on it, but while copying what the instructor was typing I stumbled upon on this problem whenever I try to run this code on my virtual or physical device it doesn't load the image.所以我刚刚开始学习 flutter 并且我正在观看有关它的课程,但是在复制讲师正在输入的内容时,每当我尝试在我的虚拟或物理设备上运行此代码时,我偶然发现了这个问题,它不会加载图像。 I've checked pubsec.yaml file and everything seems to be working fine but the image doesn't load.我检查了 pubsec.yaml 文件,一切似乎都运行良好,但图像无法加载。 Btw I'm using AndroidStudio the latest version (canary I guess).顺便说一句,我正在使用最新版本的 AndroidStudio(我猜是金丝雀)。 Thanks in advance I hope someone kind will help me.在此先感谢我希望有人能帮助我。 Here is the code:这是代码:

import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        backgroundColor: Colors.blueGrey,
        appBar: AppBar(
          title: Center(
            child: Text('I am rich.'),
          ),
          backgroundColor: Colors.blueGrey[900],
        ),
        body: Center(
          child: Image(
            image: AssetImage('diamond.png'),
          ),
        ),
      ),
    ),
  );
}

And here is a download link for the image in the code: https://drive.google.com/uc?export=download&id=1IA7othwL6jGtvc1Wfyj0K6hxrEH9OHam这是代码中图像的下载链接: https : //drive.google.com/uc?export= download &id=1IA7othwL6jGtvc1Wfyj0K6hxrEH9OHam

When you have local files that you want to use within your code, you need to remember to add it into the pubspec.yaml file under the assets section.当您有想要在代码中使用的本地文件时,您需要记住将其添加到资产部分下的pubspec.yaml文件中。 Here is an example of me registering a couple images over there:这是我在那里注册几张图片的示例:

flutter:
  ...
  assets:
    - diamond.png
    - gemstones/emerald.jpg

Please note that files (and folders) containing assets you wish to use should be registered in the root folder of your app and not your lib folder.请注意,包含您希望使用的资产的文件(和文件夹)应注册在您的应用程序的根文件夹中,而不是您的 lib 文件夹中。

Did you put the path of your image in the pubspec.yaml ?您是否将图像的路径放在pubspec.yaml For the Flutter to recognizes your asset image, you need to put the path of him in the pubspec.yaml file of your project.为了让 Flutter 识别你的资产图片,你需要将他的路径放在你项目的pubspec.yaml文件中。

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

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