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

oewdyzsn  于 2023-10-13  发布在  其他
关注(0)|答案(8)|浏览(174)

在写这篇文章之前,我到处寻找我的错误,但我什么也没找到。
尽管我已经把所有的东西都正确地写在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

下面是字体所在的路径:
path screenshot
你能帮帮我吗?我就是找不到解决办法
问候

kmbjn2e3

kmbjn2e31#

assets文件夹应位于根路径(/)中。如果您想将其保存在lib文件夹中,请将lib放在资产前面,如下所示:

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

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

643ylb08

643ylb082#

如果你的字体没有font-family,那么试试这个:
1.创建资源文件夹
1.将字体文件移动到assets文件夹下。
1.给予一个简单的名称给你的字体文件。就像没有特殊的字符或帽子。
1.在pubspec.yaml中添加以下代码

fonts:
    - family: CustomFamilyName
      fonts:
        - asset: assets/fontfile.ttf
  1. run pub get
    1.停止应用程序并重新启动它。
xu3bshqb

xu3bshqb3#

我也犯了和你一样的错误,正确指定文件夹的位置,如果是在lib文件夹中,你必须把路径写为:

lib/assets/fonts/Lato-Regular.ttf

正如问题中所述。
有些用户不喜欢这种方式,他们宁愿在库外创建文件夹。

3ks5zfa0

3ks5zfa04#

assets文件夹需要位于根目录中,而不是lib文件夹或任何其他文件夹中。
缩进规则适用,因此请检查每个部分是否正确排列:

pubspec.yaml的缩进规则

  • uses-material-design = 2空格
  • 资产= 2个空间
    • 图像/ = 4个空格
  • 字体:= 2个空格
    • 家庭:= 4个空间
  • 字体:= 6个空格
    • 资产:= 8个空间
n6lpvg4x

n6lpvg4x5#

只要检查缩进更多的时间,像制表符和空格,所有我会建议复制代码从flutter-font-declaration,然后进行更改。还有一件事是,如果你的字体在lib/assets/fonts文件夹中,你应该在yaml文件中提到从根目录开始的总路径。Happy debugging:)

3okqufwl

3okqufwl6#

我认为我们有相同的问题和我在udemy教程中遵循的确切代码。我的替代方案是使用谷歌字体在线打火机项目。尝试访问此网站,您将获得有关如何使用自定义字体的信息。https://pub.dev/packages/google_fonts

pengsaosao

pengsaosao7#

我也有同样的问题。我的问题是,我写了我的Flutter应用程序代码首先在Windows上,所以在pubspec。yaml我写的路径字体文件夹使用反斜杠,现在我试图运行我的代码在Linux系统上使用正斜杠的路径,而不是反斜杠(这也是与Mac OSX的情况下)

8hhllhi2

8hhllhi28#

我们通常将资源放在资源的目录中。

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

当然,你可以把你的资源放在任何你想要的地方。但是在你的pubspec.yaml中你需要填写相应的路径。

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

  ....

相关问题