簡體   English   中英

顯示第一個顫動圖像,但不顯示第二個

[英]First flutter image displays, but not the second

問題

TMORA.png 未顯示。

演練

代碼

  • 圖片出現在我的代碼中:

錯誤

  • 顫振拋出的錯誤:

資產圖像/TMORA.png不存在。 嘗試創建文件或修復文件路徑。 飛鏢(asset_does_not_exist)[51,8]

  • 不過,該圖像確實存在。 當我單擊資產/圖像文件夾時,我可以看到它。

在此處輸入圖片說明

  • 我相信問題源於我的 yaml 文件(如下所示)。

yaml文件

  • 完整的代碼(將 TMORA.png 行注釋掉以消除錯誤)出現在Github 上

注意:我在這個包中有兩個名為 images 的文件夾。 一個是項目資產文件夾,其中包含一個圖像文件夾,其中包含要在應用程序屏幕上顯示的 2 個圖像。 第二個是圖像文件夾,其中包含我的 github 自述文件的圖像。 我混淆了嗎?

  • pubspec.yaml 文件:

 name: second_app_Heather description: A new Flutter project. # The following line prevents the package from being accidentally published to # pub.dev using `pub publish`. This is preferred for private packages. publish_to: 'none' # Remove this line if you wish to publish to pub.dev # The following defines the version and build number for your application. # A version number is three numbers separated by dots, like 1.2.43 # followed by an optional build number separated by a +. # Both the version and the builder number may be overridden in flutter # build by specifying --build-name and --build-number, respectively. # In Android, build-name is used as versionName while build-number used as versionCode. # Read more about Android versioning at https://developer.android.com/studio/publish/versioning # In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion. # Read more about iOS versioning at # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html version: 1.0.0+1 environment: sdk: ">=2.7.0 <3.0.0" dependencies: flutter: sdk: flutter # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^0.1.3 dev_dependencies: flutter_test: sdk: flutter # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec # The following section is specific to Flutter. 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: - images/ - images/museumInterior.png - images/TMORA.png #- images/TMORA.png # - images/a_dot_ham.jpeg # An image asset can refer to one or more resolution-specific "variants", see # https://flutter.dev/assets-and-images/#resolution-aware. # For details regarding adding assets from package dependencies, see # https://flutter.dev/assets-and-images/#from-packages # To add custom fonts to your application, add a fonts section here, # in this "flutter" section. Each entry in this list should have a # "family" key with the font family name, and a "fonts" key with a # list giving the asset and other descriptors for the font. For # example: # fonts: # - family: Schyler # fonts: # - asset: fonts/Schyler-Regular.ttf # - asset: fonts/Schyler-Italic.ttf # style: italic # - family: Trajan Pro # fonts: # - asset: fonts/TrajanPro.ttf # - asset: fonts/TrajanPro_Bold.ttf # weight: 700 # # For details regarding fonts from package dependencies, # see https://flutter.dev/custom-fonts/#from-packages

編輯:

為了回應@Anis R. 的建議(因為我想很多人會提出這個建議),我已經努力實施該建議並收到以下錯誤:

Anis R. 建議 1a

Anis R. 建議 1b

Anis R. 建議 1c

編輯#2。 回復 Anis R 的第二條有用評論。

這就是問題所在... Internet 資源對於在 iPhone/Android 按鈕、背景和其他小部件上放置要使用的圖像的位置有點不切實際。 我不確定什么是常規/正確的。

一些來源建議將照片放在根目錄內的圖像文件夾中( 此處為橙色箭頭示例推薦)。 一些來源建議將照片放在資產文件夾中(綠色箭頭此處的建議示例- 參見第 1 小時,第 43 分鍾)。 有些人建議將照片放在資產文件夾內的圖像文件夾中(橙色箭頭 - 這是我嘗試的)。

不同的文件位置

因此,當我采用 Anis R 的代碼建議(我認為遵循約定)時,我收到了一條錯誤消息,解釋了問題所在。

錯誤

現在,這對我來說似乎是一個難以克服的錯誤。 如果 .yaml 文件找不到資產文件夾,那么我就很難過。 我是新手,我認為我無法解決這個問題。 我以前肯定見過這個錯誤。

這讓我想知道,“好吧,如果它看不到資產文件夾,那么它到底是怎么顯示museumInterior.png 照片的?” 兩張照片都位於同一個文件夾中。 還是他們?

在此處輸入圖片說明

不。 它從未從 assets\\images 文件夾中提取圖像。 它不知道 assets\\images 文件夾是否存在。 它一直在從我為 github README.md 存儲照片的圖像文件夾中提取照片。 里面有很多東西。 名為museumInterior.png 的圖像恰好也在那里。 我將 TMORA.png 的副本放在圖像文件夾中,它會顯示在我的屏幕上。

我和 Visual Studio Code 一樣,會假裝資產文件夾,里面有一個圖像文件夾,甚至不存在。

我不認為這是慣例,但它確實運行正常。

非常感謝@Anis R. 帶我完成了這個! 有時你只需要說出來。

在您的pubspec.yaml ,您應該使用:

- assets/images/

代替

- images/

編輯關於您的更新

我回顧了您的代碼並更新了注釋,您需要在幾個地方更改一些代碼。

  • 首先,在Home_widget.dart ,替換Image.asset('images/museumInterior.png')Image.asset('images/TMORA.png')

分別使用Image.asset('assets/images/museumInterior.png')Image.asset('assets/images/TMORA.png')

  • 然后在您的pubspec.yaml文件中的assets: ,只保留條目- assets/images/並刪除其他條目。

  • 最后,在您的項目上運行flutter clean並重建。

如果這有幫助,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM