Matjaz
Matjaz

Reputation: 484

Flutter package dependency does not load it's own assets

We have the following structure:

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 is a flutter app, that builds ios and android app. It is fully functional and everything works as expected.

Now the issues that we are having is with the project B.

Project B contains simple starter class, that sets some base property that Project A is using and will set it's own icon once we manage to execute it properly. It will also produce ios and android app.

When we start the app, there are no assets (images) recognized and we have exception:

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 content:

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/

Usually if dependency has it's own assets it should handle them on their own. But because we have to use git project, it is probably a bit different, since it is not package on it's own, but is compiled with the current project (Project B). That's why it does not find the assets.

What would be the best and the most correct approach to solving this issue? Thanks!

Upvotes: 3

Views: 2468

Answers (5)

Gregor Gračnar
Gregor Gračnar

Reputation: 28

We fixed the problem by having our assets as a full flutter project in a repository that we referenced as a dependency in pubspec.yaml

<package_name>:
    git:
      url: <your git repo here>

and then we called them by using the following widget

Image(image: AssetImage(widget.imgPath, package: "<package_name>"))

this is what our asset repo looks like

Upvotes: 1

Buhaescu Vlad
Buhaescu Vlad

Reputation: 88

I might have a solution using flutter_gen package. I managed to strip it down to not having this structure in the pubspec.yaml file of the dependency package, in your case package A:

flutter_gen:
  assets:
    outputs:
      package_parameter_enabled: true

After adding this I was able to see my pdf file. [that was my use case]

Also I managed to load the proper pdf asset file after passing the package name in the path for the asset like so considering syncfusion_flutter_pdfviewer package:

SfPdfViewer.asset('packages/{name_of_your_package}/assets/pdf_resource.pdf')

Upvotes: 1

Milad Ahmadi
Milad Ahmadi

Reputation: 1087

1- create assets folder in same level of lib folder in flutter package and put your fonts or images

enter image description here

2- add your assets path in pubspec.yaml

flutter:
assets:
- assets/svg/

3-set package name when you want to create widget

enter image description here

Upvotes: 7

Vishal_VE
Vishal_VE

Reputation: 2137

// 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

Upvotes: 0

Zaid Soomro
Zaid Soomro

Reputation: 151

I am not sure how two integrated projects work of flutter, so I apologize in advance if this is a stupid response but are you aware that the asset dependency lines (last two lines) in pubspec.yaml are commented out.

Upvotes: 0

Related Questions