Search code examples
flutterassets

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!


Solution

  • 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