FlutterError: Unable to load asset

后端 未结 22 3270
死守一世寂寞
死守一世寂寞 2020-11-30 10:31

This is the folder structure of my app

.idea
.vscode
android
build
fonts
 Oxygen-Bold.tff
 Oxygen-Light.tff
 Oxygen-Regular.tff
images
 pizza0.png
 pizza1.pn         


        
相关标签:
22条回答
  • 2020-11-30 11:12

    You should consider the indentation for assets

    flutter:
    
      assets:
        - images/pizza1.png
        - images/pizza0.png
    

    More details:

    flutter:
    
    [2 whitespaces or 1 tab]assets:
    [4 whitespaces or 2 tabs]- images/pizza1.png
    [4 whitespaces or 2 tabs]- images/pizza0.png
    
    0 讨论(0)
  • 2020-11-30 11:12

    I also had this problem. I think there is a bug in the way Flutter caches images. My guess is that when you first attempted to load pizza0.png, it wasn't available, and Flutter has cached this failed result. Then, even after adding the correct image, Flutter still assumes it isn't available.

    This is all guess-work, based on the fact that I had the same problem, and calling this once on app start fixed the problem for me:

    imageCache.clear();
    

    This clears the image cache, meaning that Flutter will then attempt to load the images fresh rather than search the cache.

    PS I've also found that you need to call this whenever you change any existing images, for the same reason - Flutter will load the old cached version. The alternative is to rename the image.

    0 讨论(0)
  • 2020-11-30 11:12

    inside pubspec.yaml, DON'T USE TAB!

    flutter:
    <space><space>assets:
    <space><space><space><space>assets/
    

    example:

    flutter:
      assets:
        assets/
    
    0 讨论(0)
  • 2020-11-30 11:12

    My issue was nested folders.

    I had my image in assets/images/logo/xyz.png and thought that - assets/images/ would catch all subfolders.

    You have to explicitly add each nested subfolder

    Solution: - assets/images/logo/ etc.

    0 讨论(0)
  • 2020-11-30 11:14

    I had the same issue I corrected it, you just need to put the two(uses-material-design: true and assets) in the same column and click in the upgrade dependencies but before restart android studio.

    screenshot

    0 讨论(0)
  • 2020-11-30 11:16

    Flutter uses the pubspec.yaml file, located at the root of your project, to identify assets required by an app.

    Here is an example:

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

    To include all assets under a directory, specify the directory name with the / character at the end:

    flutter:
      assets:
        - directory/
        - directory/subdirectory/
    

    For more info, see https://flutter.dev/docs/development/ui/assets-and-images

    0 讨论(0)
提交回复
热议问题