Search code examples
flutterdartsvgassets

Flutter Dart: can't load SVG assets: Unable to load asset iconPath


I am trying to use the flutter_svg package to load some .svg icon.

class _MyHomePageState extends State<MyHomePage> {
  final String iconPath = "assets/icons/adept.svg";
  ...
}

I added the assets/ folder containing the icons/ folder to the pubspec.yaml file:

  assets:
    - assets/

And when I try to load the icon inside my body:

      body: Center(
        child: Container(
          child: LimitedBox(
            child: SvgPicture.asset('iconPath', color: Colors.black, width: 100, height: 100,),
            maxHeight: 100,
            maxWidth: 100,
          )
        )
      ),

I get this StackTrace, Unable to load asset: assetName,

I/flutter (10154): ══╡ EXCEPTION CAUGHT BY SVG ╞═══════════════════════════════════════════════════════════════════════
I/flutter (10154): The following assertion was thrown resolving a single-frame picture stream:
I/flutter (10154): Unable to load asset: iconPath
I/flutter (10154): 
I/flutter (10154): When the exception was thrown, this was the stack:
I/flutter (10154): #0      PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:223:7)
...
I/flutter (10154): Picture provider: ExactAssetPicture(name: "iconPath", bundle: null, colorFilter: null)
I/flutter (10154): Picture key: AssetBundlePictureKey(bundle: PlatformAssetBundle#153a9(), name: "iconPath",
I/flutter (10154):   colorFilter: null)
I/flutter (10154): ════════════════════════════════════════════════════════════════════════════════════════════════════

Kindly explain to me what am I missing?


Solution

  • Did you change like below code?

    assets:
        - assets/
        - assets/icons/
    
          body: Center(
            child: Container(
              child: LimitedBox(
                child: SvgPicture.asset(iconPath, color: Colors.black, width: 100, height: 100,),
                maxHeight: 100,
                maxWidth: 100,
              )
            )
          ),