在我写这篇文章之前,我到处搜索了我的错误,但是没有发现任何东西。
尽管我一切正确,并记在pubspec.yaml文件中,但在调试控制台中却收到此错误:
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)
我的pubspec.yaml文件如下:
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
这是字体在其中的路径:
也许可以帮我吗? 我只是找不到解决方法
最好的问候
答案 0 :(得分:1)
如果您的字体没有字体家族,请尝试使用此字体:
fonts:
- family: CustomFamilyName
fonts:
- asset: assets/fontfile.ttf
运行pub get
停止应用并重新启动。
答案 1 :(得分:0)
资产文件夹应位于根路径(/)中。如果要将其保留在lib
文件夹中,请将lib
放在资产前面,如下所示:
//new path
lib/assets/fonts/Lato-Regular.ttf
旁注:为避免必须手动导入字体,我建议使用google_fonts软件包。它具有包括Lato在内的数百种字体,您可以使用GoogleFonts.lato()
访问它以获得Lato字体的默认TextStyle
,而不必通过pubspec.yml
文件导入。
答案 2 :(得分:0)
我也犯了与您相同的错误,请正确指定文件夹的位置,如果位于lib文件夹中,则必须将路径写为:
lib/assets/fonts/Lato-Regular.ttf
如问题所述。
一些用户不喜欢这种方式,他们宁愿在lib之外创建文件夹。
希望它回答了您的问题。
答案 3 :(得分:0)
只需检查更多时间的缩进,例如制表符和空格以及所有 我建议从flutter-font-declaration复制代码,然后对其进行更改。 还有一件事是,如果字体在lib / assets / fonts文件夹中,则应在yaml文件中提及根目录的总路径。 调试愉快:)
答案 4 :(得分:0)
我认为我们有我在 udemy 教程中遵循的相同问题和确切代码。我的替代方法是在线使用谷歌字体进行更轻的项目。尝试访问此站点,您将获得有关如何使用自定义字体的信息。 https://pub.dev/packages/google_fonts
答案 5 :(得分:0)
我遇到了同样的问题。我的问题是我首先在 Windows 上编写了我的 flutter 应用程序代码,所以在 pubspec.yaml 中我使用反斜杠写了字体文件夹的路径,现在我试图在 linux 系统上运行我的代码,该系统使用正斜杠代替反斜杠作为路径( mac OSX 也是如此)
所以检查你是否有同样的问题?