代码之家  ›  专栏  ›  技术社区  ›  arcthurus

无法加载具有包结构的资产

  •  0
  • arcthurus  · 技术社区  · 11 月前

    我的项目使用了一个使用melos的包结构。我有个包裹 ui_components 其中包括ui套件和资产。资源包含具有不同功能的图像的文件夹:游戏、图标、公文包等。

    我还有包裹 game 。它拍摄的图像来自 ui_components/assets/game 。但某些图像未加载:

    Another exception was thrown: Unable to load asset: "assets/game/idv_short.webp".
    

    Dir ui组件/资产/游戏 是新的。如果我将图像从游戏移动到其他预先存在的文件夹,如公文包,则图像加载良好。 在 ui组件 包在 pubspec.yaml 我指定了:

    flutter:
      uses-material-design: true
      assets:
        ...
        - assets/game/
    

    在我导入的游戏包中:

    ui_components: 0.0.1
    

    在软件包/ui_components/lib/regenerated/assets.gen.dart中,一切正常:

      /// File path: assets/game/idv_short.webp
      AssetGenImage get idvShort => 
          const AssetGenImage('assets/game/idv_short.webp');
    

    我如何使用它:

    BoxDecoration(
        image: DecorationImage(
            image: AssetImage(Assets.weeksGame.idvShort.path),
                fit: BoxFit.cover,
         ),
     ),
    

    我错过了什么?

    2 回复  |  直到 11 月前
        1
  •  0
  •   harsh bangari    11 月前

    尝试以这种方式添加资产:

    pubspec.yaml

      assets:
        - assets/images/ic_app_logo.png
        - assets/images/ic_app_logo_new.png
        - assets/images/ic_attachment.png
        - assets/images/ic_close.png
    

    widget.dart

                           Container(
                          child: const Image(
                              fit: BoxFit.contain,
                              image:
                                  AssetImage('assets/images/ic_app_logo.png')),
                        ),
    

    和从包加载图像( ui_components )依赖项,包参数必须提供给AssetImage。

     const AssetImage('assets/game/ic_app_logo.png', package: 'ui_components');
    
        2
  •  0
  •   Muhammad Raheel Anwar    11 月前

    可能有一些事情需要检查并确保资产能够正确加载。我将建议一些步骤来排除故障并解决此问题:

    1. 确保中的路径正确 pubspec.yaml : 请确保中指定的路径 pubspec.yaml ui_components 包是正确的,包括 assets/game/ 目录

      flutter:
        uses-material-design: true
        assets:
          - assets/game/
      
    2. 检查打字 : 确保中指定的路径中没有拼写错误 pubspec.yaml 以及您使用资产的代码。

    3. flutter pub get : 对进行更改后 pubspec.yaml 扑腾酒吧 以确保应用这些更改。

    4. 验证中的资产路径 generated/assets.gen.dart : 确保生成的文件 assets.gen.dart 正确反映资产的路径。

      /// File path: assets/game/idv_short.webp
      AssetGenImage get idvShort => 
          const AssetGenImage('assets/game/idv_short.webp');
      
    5. 检查代码中的正确用法 : 确保您在代码中正确使用了资产。

    6. 确保生成中包含资产 : 确保资产包含在生成过程中。有时,如果未正确指定路径,则可能无法正确复制资源。

    7. 检查平台特定问题 : 有时,特定于平台的问题可能会导致资产无法加载。确保资产在目标平台上被正确格式化和支持(例如。, .webp 文件可能具有特定要求)。

    8. 清洁和重建 : 跑 flutter clean 然后 flutter build 以确保构建过程正确地包括所有资产。

    通过以下步骤,您应该能够解决问题。