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

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

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

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

我将包含 icons/ 文件夹的 assets/ 文件夹添加到 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,
          )
        )
      ),

只做 3 个工作: 运行 终端中的这个命令:flutter clean。

然后在 android 工作室转到文件并 select: 缓存无效并重新启动。

然后通过这个命令再次获取依赖:flutter pub get