简体   繁体   中英

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:

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

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.
  2. Then I have defined image resources in pubspec.yaml file like below image在此处输入图片说明

  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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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