Reputation: 365
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:
Can you help me maybe? I just can't find a solution
best regards
Upvotes: 26
Views: 39182
Reputation: 2780
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.
Upvotes: 0
Reputation: 3338
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
....
Upvotes: 0
Reputation: 1032
If there is no font-family for your font, then try using this:
fonts:
- family: CustomFamilyName
fonts:
- asset: assets/fontfile.ttf
run pub get
stop your app and restart it.
Upvotes: 6
Reputation: 2627
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
Upvotes: 2
Reputation: 1
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 )
Upvotes: 0
Reputation: 73
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.
Upvotes: 3
Reputation: 15
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
Upvotes: 1
Reputation: 57
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 :)
Upvotes: 2
Reputation: 6161
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.
Upvotes: 45