Error: unable to locate asset entry in pubspec.yaml: "assets/fonts/Lato-Regular.ttf"
Asked Answered
D

9

26

Before I wrote this, I searched everywhere for my mistake, but I didn't find anything.

although I have everything correctly and written down in the pubspec.yaml file, I get this error in the debug console:

Launching lib/main.dart on Android SDK built for x86 in debug mode...
Error: unable to locate asset entry in pubspec.yaml: "assets/fonts/Lato-Regular.ttf".
✓ Built build/app/outputs/apk/debug/app-debug.apk.
Error: unable to locate asset entry in pubspec.yaml: "assets/fonts/Lato-Regular.ttf".
Exited (sigterm)

my pubspec.yaml file looks like this:

name: shopping_app
description: A new Flutter project.

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1

environment:
  sdk: ">=2.1.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.2

dev_dependencies:
  flutter_test:
    sdk: flutter


# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  # assets:
  #  - images/a_dot_burr.jpeg
  #  - images/a_dot_ham.jpeg

  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.dev/assets-and-images/#resolution-aware.

  # For details regarding adding assets from package dependencies, see
  # https://flutter.dev/assets-and-images/#from-packages

  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  fonts:
    - family: Lato
      fonts:
        - asset: assets/fonts/Lato-Regular.ttf
        - asset: assets/fonts/Lato-Bold.ttf
          weight: 700
    - family: Anton
      fonts:
        - asset: assets/fonts/Anton-Regular.ttf
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts from package dependencies,
  # see https://flutter.dev/custom-fonts/#from-packages

here is the path where the fonts are inside:

path screenshot

Can you help me maybe? I just can't find a solution

best regards

Duplicity answered 20/12, 2019 at 23:2 Comment(0)
H
43

The assets folder should be in the root path (/). If you want to keep it in the lib folder, put lib in front of assets like this:

//new path
lib/assets/fonts/Lato-Regular.ttf

Sidenote: To avoid having to import fonts manually, I recommend the google_fonts package. It has hundreds of fonts including Lato and you can access it with GoogleFonts.lato() to get a default TextStyle with the Lato font without having to import it via the pubspec.yml file.

Haematopoiesis answered 20/12, 2019 at 23:21 Comment(1)
Note that you should always bundle your fonts in production! Otherwise even an offline app would have to fetch the fonts from Google - which might cause legal issues in many countries (especially in the EU). See pub.dev/packages/google_fonts#bundling-fonts-when-releasingHotbox
H
6

If there is no font-family for your font, then try using this:

  1. Create an assets folder
  2. Move your font file under the assets folder.
  3. Give a simple name to your font file. Like without special characters or caps.
  4. Add this code in pubspec.yaml
fonts:
    - family: CustomFamilyName
      fonts:
        - asset: assets/fontfile.ttf
  1. run pub get

  2. stop your app and restart it.

Helix answered 4/8, 2020 at 3:5 Comment(0)
B
2

I was also committing the same mistake as you have, specify the location of the folder correctly, if is inside the lib folder you have to write the path as:

lib/assets/fonts/Lato-Regular.ttf

as stated in the problem.

Some of the users don't prefer this way, they rather make folder outside lib.

Bergquist answered 30/7, 2020 at 5:29 Comment(0)
L
2

The assets folder needs to be inside the root directory and not inside the lib folder or any other folder.

Indentation rules apply, so please check each section is lined up correctly:

Indentation Rules for pubspec.yaml

  • uses-material-design = 2 spaces

  • assets = 2 spaces

  • -images/ = 4 spaces

  • fonts: = 2 spaces

    • -family: = 4 spaces

    • fonts: = 6 spaces

    • -asset: = 8 spaces

Lacilacie answered 22/3, 2022 at 14:8 Comment(0)
T
1

Just check the indentation on more time, like tabs and spaces and all I would recommended copying the code from flutter-font-declaration and then making changes to it. And one more thing is, if your fonts are in lib/assets/fonts folder you should mention the total path from root directory in your yaml file. Happy debugging :)

Trenttrento answered 25/8, 2020 at 14:5 Comment(0)
V
0

I think we have the same issues and exact code which I followed in udemy tutorial. My alternative to this is to use google fonts online for lighter project. Try visiting this site, you will get information on how to use custom fonts. https://pub.dev/packages/google_fonts

Vizor answered 24/5, 2021 at 2:57 Comment(0)
S
0

I had same problem. My problem was that I wrote my flutter app code firstly on windows so in pubspec.yaml i wrote path to font folder using backward slash and now I am trying to run my code on linux system which usage forward slash for path instead of backward slash ( this is also case with mac OSX )

Smutchy answered 26/6, 2021 at 21:10 Comment(0)
Y
0

We usually put resources in the resource's directory.

your awesome project name 
 |- (Some automatic generation by compilation tools)
 |- android
 |- assets
 | └── images
 | └── fonts    //put your fonts here
 |
 |- ios
 |- lib
 |- linux
 |- macos
 |- test
 |- web
 |- windows

Of course, you can put your resources anywhere you want. but in your pubspec.yaml you need to fill in the corresponding path.

Code in pubspec.yaml

...

flutter:
  uses-material-design: true

  assets:
   - assets/images/           # all images will load.

  fonts: 
    - family: Lato
  fonts:
    - asset: assets/fonts/Lato-Regular.ttf   #this "- asset: assets/fonts/" will be not working
      weight: 700

  ....

  
Yuk answered 4/10, 2023 at 9:13 Comment(0)
P
0

Double check your file structure and the pubspec.yaml file. I had accidently put the fonts in /assets/fonts/Lato instead of /assets/fonts but my pubspec.yaml had this content:

fonts:
    - family: Lato
      fonts:
        - asset: assets/fonts/Lato-Black.ttf
          weight: 900        

Moving the fonts to /assets/fonts fixed the issue.

Purdy answered 21/12, 2023 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.