Flutter包依赖项不加载它自己的资产

h9vpoimq  于 2023-06-07  发布在  Flutter
关注(0)|答案(4)|浏览(142)

我们有以下结构:

project A (hosted on private git). It has it's asset folder and is loading assets like this: Image.asset("images/Logo_png_1080x1080px.png")
project B, that uses project A as dependency

Project A是一个Flutter应用程序,构建iOS和Android应用程序。它功能齐全,一切都按预期工作。
现在我们遇到的问题是项目B。
项目B包含简单的启动器类,它设置了项目A正在使用的一些基本属性,并且一旦我们设法正确执行它,就会设置它自己的图标。它还将开发iOS和Android应用程序。
当我们启动应用程序时,没有识别的资产(图像),我们有例外:

I/flutter ( 7290):
  Unable to load asset: images/Logo_png_1080x1080px.png
I/flutter ( 7290):
I/flutter ( 7290):
  When the exception was thrown, this was the stack:
I/flutter ( 7290): #0      PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:224:7)
I/flutter ( 7290): <asynchronous suspension>
I/flutter ( 7290): #1      AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:675:14)
I/flutter ( 7290): <asynchronous suspension>

Pubspec内容:

name: project_b
description: A new Flutter project.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  project_a:
    git:
      url: https://***:***@private.git.repo.com/scm/repo/git/ProjectA
      ref: master
      path: mobile_app

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^1.0.0

flutter:
  uses-material-design: true
#  assets:
#    - packages/project_a/images/

通常,如果依赖项有自己的资产,它应该自己处理它们。但是因为我们必须使用git项目,所以它可能有点不同,因为它不是自己打包的,而是与当前项目(项目B)一起编译的。这就是为什么它找不到资产。
解决这个问题的最好和最正确的方法是什么?谢谢!

uemypmqf

uemypmqf1#

1-在flutter包中的lib文件夹的同一级别创建assets文件夹,并将您的字体或图像

2-在pubspec.yaml中添加您的资产路径

3-创建widget时设置包名

ktca8awb

ktca8awb2#

我不确定flutter的两个集成项目是如何工作的,所以如果这是一个愚蠢的回应,我提前道歉,但你知道pubspec.yaml中的资产依赖行(最后两行)被注解掉了吗?

t1rydlwq

t1rydlwq3#

// you are not correctly defining the assets seen in your code under flutter uses-material-design: true assets folder should be like this 
        
         uses-material-design: true
         assets:
           - packages/project_a/images/
    
    if You do not use the proper format you will get an error, Thanks this will help you out. I am sharing my project pubsec.yml file check this 
          
        
        /////pubsec.yml file
        name: traveling
        description: A new Flutter application.
        
        # The following line prevents the package from being accidentally published to
        # pub.dev using `pub publish`. This is preferred for private packages.
        publish_to: 'none' # Remove this line if you wish to publish to pub.dev
        
        # 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.12.0 <3.0.0"
        
        dependencies:
          flutter:
            sdk: flutter
          flutter_launcher_icons: ^0.9.2
        
        
        
          # The following adds the Cupertino Icons font to your application.
          # Use with the CupertinoIcons class for iOS style icons.
          cupertino_icons: ^1.0.2
        dev_dependencies:
          flutter_test:
            sdk: flutter
        flutter_icons:
          image_path: "assets/Vector-2.png"
          android: "launcher_icon"
          ios: true
          remove_alpha_ios: true
        
        
        # 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
          assets:
            - assets/
            - assets/Vector-2.png
        
          # 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: Schyler
          #     fonts:
          #       - asset: fonts/Schyler-Regular.ttf
          #       - asset: fonts/Schyler-Italic.ttf
          #         style: italic
          #   - 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
hfyxw5xn

hfyxw5xn4#

我可能有一个使用flutter_gen包的解决方案。我设法将其简化为在依赖包的pubspec.yaml文件中没有这种结构,在您的情况下是包A:

flutter_gen:
  assets:
    outputs:
      package_parameter_enabled: true

添加此内容后,我可以看到我的PDF文件。[这是我的用例]

相关问题