简体   繁体   English

Flutter package 中的资产在应用中不可见

[英]Assets in Flutter package are not visible in the app

I've created a package, which uses some assets (sf2 file, ttf files, etc).我创建了一个 package,它使用了一些资产(sf2 文件、ttf 文件等)。 Assets don't seem to load.资产似乎没有加载。 I can fix this by including these files in the assets folder of my main project (so now, every asset in the package is also in my main project).我可以通过将这些文件包含在我的主项目的资产文件夹中来解决这个问题(所以现在,package 中的每个资产也在我的主项目中)。 However, when I build a web app, it still creates an extra folder for my package with the files that it needs.但是,当我构建一个 web 应用程序时,它仍然会为我的 package 创建一个额外的文件夹,其中包含它需要的文件。

Is there a way around this?有没有解决的办法? How do I enforce the package to use the assets from itself?如何强制 package 使用其自身的资产?

After some digging, I've found a way to include it.经过一番挖掘,我找到了一种包含它的方法。

'packages/livescore_sdk/assets/default_team_logo.png' works instead of 'assets/default_team_logo.png' 'packages/livescore_sdk/assets/default_team_logo.png'代替'assets/default_team_logo.png'

Include the assets in your lib folder, and import it from local project only.将资产包含在您的 lib 文件夹中,并仅从本地项目导入它。

Assuming the project name ABC :假设项目名称为ABC

Steps脚步
  1. Create assets folder in lib, example: lib/assets .在 lib 中创建 assets 文件夹,例如: lib/assets Full assets example url: ABC/lib/assets/some_asset.png完整资产示例 url: ABC/lib/assets/some_asset.png
  2. Copy the assets in this folder.复制此文件夹中的资产。
  3. Import the assets in your pubspec.yaml like this:像这样在 pubspec.yaml 中导入资产:
  assets:
    - packages/ABC/assets/some_asset.png

Hope this help someone.希望这对某人有帮助。 :smiley: :笑脸:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM