繁体   English   中英

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

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

我通过添加要添加的图片的文件夹路径修改了我的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代码

扑:

  # 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.

您应该尊重缩进,即使添加或删除一个空格也不会起作用,这是您应该这样做的方式:

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

最好的方法是从官方文档中复制/过去。

  1. 我在项目的根目录中创建了 assets/images 文件夹,并将 icon.png 和 gplus.png 图像文件放入其中。
  2. 然后我在 pubspec.yaml 文件中定义了图像资源,如下图在此处输入图片说明

  3. 然后在项目中使用图像,如下所示在此处输入图片说明

如果上述解决方案不起作用

尝试重新启动您的 IDE(Andriod studio/VSCode),它对我有用

暂无
暂无

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

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