Flutter Dart:无法加载 SVG 资产:无法加载资产 iconPath

问题描述

我正在尝试使用 flutter_svg 包加载一些 .svg 图标。

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

我将包含 assets/ 文件夹的 icons/ 文件添加pubspec.yaml 文件中:

  assets:
    - assets/

当我尝试在我的身体内加载图标时:

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

我得到这个 StackTrace,无法加载资产: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): ════════════════════════════════════════════════════════════════════════════════════════════════════

请向我解释我遗漏了什么?

解决方法

你有没有像下面的代码那样改变?

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,)
        )
      ),