无法在Flutter中加载图像资源。

huangapple go评论64阅读模式
英文:

unable load image asset in flutter

问题

我不确定为什么我的来自资产的图像无法正常工作。

我查看了这两个问题:

这是我尝试获取图像的方式:

Image(
    image: AssetImage('assets/logo.png'),
    width: 100,
    height: 100,
),

这是我的pubspec.yaml

assets:
    - assets/logo.png
    - assets/back.png

以下是文件结构:
这是文件结构

我尝试按照YouTube上的视频操作,但那并没有起作用。

欢迎任何建议。

英文:

I am not sure why my images from assets is not working

I looked at these two questions:

-https://stackoverflow.com/questions/64158543/flutter-unable-to-load-image-asset

-https://stackoverflow.com/questions/67552389/unable-to-load-image-asset

this is how I am trying to fetch the image:

          Image(
            image: AssetImage('assets/logo.png'),
            width: 100,
            height: 100,
          ),

here is my pubspec.yaml

 assets:
    - assets/logo.png
    - assets/back.png


below is the file structure
无法在Flutter中加载图像资源。

I tried following videos on youtube but that didnt work.

any suggestions are welcome

答案1

得分: 1

尝试以下代码并更改您的 pubspec.yaml 文件如下-

如果您的图像在图像文件夹中

 assets:
    - assets/images

如果您的图像在资产文件夹中

 assets:
    - assets/

之后运行命令 flutter pub get 并停止代码,然后重新运行

参考 assets-and-images

英文:

Try below code and change your pubspec.yaml file like-

if your images in images folder

 assets:
    - assets/images

if your images in assetsfolder

 assets:
    - assets/

after that run command flutter pub get and Stop the code and re-run again

Refer assets-and-images

答案2

得分: 1

assets:
  - assets/images/
Image(
  image: AssetImage('assets/images/logo.png'),
  width: 100,
  height: 100,
),
英文:

if your images are in images folder of assets folder then declare as below in pubspec.yaml

assets:
  - assets/images/

And write code to get image as below

Image(
        image: AssetImage('assets/images/logo.png'),
        width: 100,
        height: 100,
      ),

答案3

得分: -1

我重新启动了应用程序,然后一切正常。

英文:

I ended up restarting the app then everyting worked

huangapple
  • 本文由 发表于 2023年4月6日 21:14:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定