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

67up9zun  于 2023-09-28  发布在  Flutter
关注(0)|答案(2)|浏览(149)

我已经修改了我的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.

//我的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.
vojdkbi0

vojdkbi01#

1.我已经在项目的根目录下创建了assets/images文件夹,并将icon.png和gplus.png图像文件放入其中。
1.然后我在pubspec.yaml文件中定义了图像资源,如下图x1c 0d1x
1.然后在项目中使用的图像如下

5kgi1eie

5kgi1eie2#

你应该尊重缩进,如果你添加或删除甚至一个空格,它都不会起作用,这是你应该做的:

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

最好的方法是从official documentation复制/传递它。

相关问题