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

时间:2021-01-20 09:33:54

标签: flutter dart svg assets

我正在尝试使用 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): ════════════════════════════════════════════════════════════════════════════════════════════════════

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

1 个答案:

答案 0 :(得分:0)

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

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