简体   繁体   English

当我尝试访问 Flutter 中的图像/资产时遇到问题

[英]I have a problem when i try to access the image /assets in Flutter

Did you tried to add your image in asset folder in your project directory?您是否尝试将图像添加到项目目录的资产文件夹中?

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

Make sure you have create assets folder in your project working directory, put your images in assets folder and write below code in your pubspec.yaml file.确保在项目工作目录中创建了资产文件夹,将图像放在资产文件夹中,并在pubspec.yaml文件中写入以下代码。

Refer adding assets in your project here请在此处参考在您的项目中添加资产

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

assets:
    - assets/

Your Widget:你的小工具:

Image.asset('assets/human.jpg'),

1- make sure you write image path correctly 1-确保正确写入图像路径

2- write this commend in terminal 2- 在终端中写这个表扬

flutter pub get

or if you use vsCode ope pubspec.yaml and click Ctrl + s或者如果你使用 vsCode ope pubspec.yaml 然后点击 Ctrl + s

Change your pubspec.yaml file更改您的 pubspec.yaml 文件

from

 - assets/human.jpg

to this对此

assets:
    - assets/

and your和你的

child : Image(
   image : AssetImage("assets/human.jpg")
   )
)

and finally make sure your space before asset最后确保您在资产之前的空间

 (2 empty spces or 1 tab)    assets:
  (4 empty spces or 2 tabs)      - assets/

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

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