Flutter - different asset path in package's example when building

137 views Asked by At

When creating a custom package that contains font as a .ttf file. I try to download the font file also in the example app that is contained in the package.

The font is downloaded successfully on iOS and macOS apps but not on Linux or Web.

The web console is giving me an error Failed to load font My-Custom-Icons at assets/../lib/assets/fonts/My-Custom-Icons.ttf

...

my_package:
  

    # When depending on this package from a real application you should use:
    #   custom_package: ^x.y.z
    # See https://dart.dev/tools/pub/dependencies#version-constraints
    # The example app is bundled with the plugin so we use a path dependency on
    # the parent directory to use the current plugin's version.
    path: ../

...


flutter:
  uses-material-design: true
  
  fonts:
    - family: My-Custom-Icons
      fonts:
        - asset: ../lib/assets/fonts/My-Custom-Icons.ttf


I would expect that font would be downloaded the same way on all different platforms. What I'm missing here?

2

There are 2 answers

1
Suraj Mahendrakar On

Hi just replace this and try.

- asset: assets/fonts/My-Custom-Icons.ttf
0
Niko Lipponen On

Ah, it seems that you can use the packages path when importing the font even inside the example. So the following did the work.

...

my_package:
  

    # When depending on this package from a real application you should use:
    #   custom_package: ^x.y.z
    # See https://dart.dev/tools/pub/dependencies#version-constraints
    # The example app is bundled with the plugin so we use a path dependency on
    # the parent directory to use the current plugin's version.
    path: ../

...


flutter:
  uses-material-design: true
  
  fonts:
    - family: My-Custom-Icons
      fonts:
        - asset: packages/my_package/assets/fonts/My-Custom-Icons.ttf