错误:无法在 pubspec.yaml 中找到资产条目:“assets/fonts/Lato-Regular.ttf”

新手上路,请多包涵

在我写这篇文章之前,我到处寻找我的错误,但我没有找到任何东西。

尽管我在 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

这是字体所在的路径:

路径截图

你能帮我吗?我只是找不到解决方案

最好的祝福

原文由 Tolga 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.3k
2 个回答

资产文件夹应位于根路径 (/) 中。如果你想把它放在 lib 文件夹中,把 lib 放在像这样的资产前面:

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

旁注:为了避免手动导入字体,我推荐使用 google_fonts 包。它有数百种字体,包括 Lato,您可以使用 GoogleFonts.lato() 访问它以获得默认值 TextStyle 使用 Lato 字体,而无需通过 pubspec.yml 文件导入.

原文由 Benjamin 发布,翻译遵循 CC BY-SA 4.0 许可协议

如果您的字体没有字体系列,请尝试使用此字体:

  1. 创建资产文件夹
  2. 将您的字体文件移动到 assets 文件夹下。
  3. 为您的字体文件起一个简单的名称。就像没有特殊字符或大写字母一样。
  4. 在 pubspec.yaml 中添加此代码
fonts:
    - family: CustomFamilyName
      fonts:
        - asset: assets/fontfile.ttf

  1. 运行 pub get

  2. 停止您的应用程序并重新启动它。

原文由 NullByte08 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题