简体   繁体   English

如何在flutter上的pubspec.yaml文件上添加图片路径

[英]how to add a picture path on pubspec.yaml file on flutter

I have modified my pubspec.yaml file by adding the folder path of the picture that I want to add but I an error:我通过添加要添加的图片的文件夹路径修改了我的pubspec.yaml文件,但出现错误:

Running "flutter pub get" in drawer2...                         
Error on line 44, column 5 of pubspec.yaml: Expected a key while parsing a block mapping.

   ╷

44 │     assets :

   │     ^

   ╵

Oops; flutter has exited unexpectedly.
Sending crash report to Google.
Crash report sent (report ID: 7f0c2ad768060692)
Crash report written to C:\Users\Mohamed bh\Desktop\files\flutter operations\drawer2\flutter_06.log;
please let us know at https://github.com/flutter/flutter/issues.

//My pubspec.yaml code //我的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/a_dot_burr.jpeg
   # - images/a_dot_ham.jpeg

Running "flutter pub get" in drawer2...                         
Error on line 44, column 5 of pubspec.yaml: Expected a key while parsing a block mapping.

   ╷

44 │     assets :

   │     ^

   ╵

Oops; flutter has exited unexpectedly.
Sending crash report to Google.
Crash report sent (report ID: 7f0c2ad768060692)
Crash report written to C:\Users\Mohamed bh\Desktop\files\flutter operations\drawer2\flutter_06.log;
please let us know at https://github.com/flutter/flutter/issues.

You should respect the indentation, it won't work if you add or remove even one space, this is how you should do it:您应该尊重缩进,即使添加或删除一个空格也不会起作用,这是您应该这样做的方式:

flutter:
  assets:
    - assets/my_icon.png
    - assets/background.png

The best way is to copy/past it from the official documentation .最好的方法是从官方文档中复制/过去。

  1. I have created assets/images folder in root of the project and placed icon.png and gplus.png images files in to it.我在项目的根目录中创建了 assets/images 文件夹,并将 icon.png 和 gplus.png 图像文件放入其中。
  2. Then I have defined image resources in pubspec.yaml file like below image然后我在 pubspec.yaml 文件中定义了图像资源,如下图在此处输入图片说明

  3. Then used image in project like below然后在项目中使用图像,如下所示在此处输入图片说明

If the above solutions did not work如果上述解决方案不起作用

Try to restart your IDE(Andriod studio/VSCode), its work for me尝试重新启动您的 IDE(Andriod studio/VSCode),它对我有用

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

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