Error on retrieving assets for web (flutter)

1,938

Since the status of flutter for web is still preview, assets handling is not the same as for mobile.assets folder should be in the web folder, and reference path for the image should not include assets:

Image.asset("images/my_custom_image.png")

In flutter for web, we do not need to reference the assets in the pubspec.yaml.

Share:
1,938
Aurimas Deimantas
Author by

Aurimas Deimantas

Updated on December 13, 2022

Comments

  • Aurimas Deimantas
    Aurimas Deimantas over 1 year

    I face an issue for Flutter WEB. I followed tutorial on assets from Flutter team (https://flutter.dev/docs/development/ui/assets-and-images) however I receive an error

     ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
    The following assertion was thrown resolving an image codec:
    Unable to load asset: assets/images/my_custom_image.png
    
    When the exception was thrown, this was the stack:
    dart:sdk_internal 4602:11                                  throw_
    load
    package:flutter_web/…/services/asset_bundle.dart:220
    dart:sdk_internal 25309:9                                  <fn>
    ...
    
    Image provider: AssetImage(bundle: null, name: "assets/images/my_custom_image.png")
    Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#078a9(), name:
      "assets/images/my_custom_image.png", scale: 1)
    ════════════════════════════════════════════════════════════════════════════════════════════════════ 
    

    My pubspecs.yaml

    flutter:
      uses-material-design: true
    
      assets:
        - assets/images/
    

    I'm loading image like this

    Image.asset("assets/images/my_custom_image.png")
    

    Where did I mess up?

  • Aurimas Deimantas
    Aurimas Deimantas over 4 years
    If I move assets folder under web, pubspec.yaml complains that The asset directory assets/images/ does not exist. Try creating the directory or fixing the path to the directory If I keep folder in global path, it doesn't complain
  • Aurimas Deimantas
    Aurimas Deimantas over 4 years
    Got it working. I removed everything from yaml and added path, as you suggested. Thanks @Spatz!
  • xcsob
    xcsob over 3 years
    How would you handle the assets inside a thirdy party package?
  • EngineSense
    EngineSense over 3 years
    It's totally opposite case in my flutter project. assets/images/some.jpg won't work in local, But on remote it works. Strange path by flutter.