【问题标题】:Image couldn't be loaded from asset folder(No indentation problem)无法从资产文件夹加载图像(无缩进问题)
【发布时间】:2020-12-17 18:16:25
【问题描述】:

我在终端上使用这个命令创建了一个新项目:
flutter create test_app

然后,我创建了 assets 文件夹和 images 文件夹。我已在此文件夹中添加了一张图片进行测试。

test_app/assets/images/

test_app/assets/images/note.png

我已经为 assets 文件夹编辑了 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/

然后,我将此行添加到 main.dart 文件中包含一些小部件的列中。

Image(image: AssetImage('images/note.png')),

我做的一切都是正确的,但为什么它不起作用我无法得到它?我过去做过一些项目,没有问题。我尝试创建许多项目来解决这个问题,但我做不到。

Unable to load asset: images/note.png

...
...

Image provider: AssetImage(bundle: null, name: "images/note.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#e7b4d(), name: "images/note.png", scale: 1.0)
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 1.22.5, on Mac OS X 10.15.6 19G2021 darwin-x64,
    locale en-TR)
 
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[✓] Xcode - develop for iOS and macOS (Xcode 12.0)
[✓] Android Studio (version 4.0)
[✓] VS Code (version 1.52.0)
[✓] Connected device (1 available)

• No issues found!

iOS模拟器和Android手机我都试过了。

我的电脑是 macOS Catalina 10.15.6。

谢谢...

【问题讨论】:

    标签: flutter flutter-dependencies


    【解决方案1】:

    你需要像这样给出图片的完整路径:

      Image(image: AssetImage('assets/images/note.png')),
    

    【讨论】:

    • 非常感谢,这是我的错。正如你所说,我已经改变了它并且它有效。 :)?
    【解决方案2】:

    尝试将图像也添加到 pubspec.yaml 文件夹中,而不仅仅是 assets/images/。我的意思如下:

      # To add assets to your application, add an assets section, like this:
      assets:
        - assets/images/image.png
    

    这会起作用的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-23
      • 2020-08-10
      相关资源
      最近更新 更多