簡體   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