Open ammarhemani opened 3 years ago
Hi @ammarhemani
I think this is working as intended, you need to assets relative pubspec.yaml not outside of the directory where pubspec.yaml
isn't located. Please read https://flutter.dev/docs/development/ui/assets-and-images#asset-bundling
Closing, as this isn't an issue with Flutter itself. If you disagree, please write in the comments and I will reopen it.
Thank you
The documentation just says that the assets path needs to be specified relative to the pubspec.yaml, and it doesn't say anywhere that the assets can't be outside of the project directory.
Just to confirm then, assets cannot be stored outside of the project directory in any way?
If that's the case, can you suggest a way I can reuse the same general assets between different applications?
P.S: The relative assets path outside of the project directory is working perfectly fine on iOS, but it doesn't work on the first launch for Android. As soon as I hot-reload, that works fine too. The problem is on the first launch in Android, it gives an error 'Unable to load asset.'
Thanks
@ammarhemani Thanks, that's a good point, I took it as it should be relative to pubspec.yaml in the project itself.
I can reproduce the issue on Android
assets:
- ../images/
Any updates or workarounds?
it's 2023, G-men, this is still an issue with 3.7.1, my project setup for assets is using a relative path, iOS can read the svg image no issue, but android threw an error claiming unable to read the image file, 😭
This is necessary for white label apps using a base application that holds all your assets that you import to each white label app. Any update on this?
I found a workaround for people with an issue here. You can actually create a separate project to house the images and then import that project to use the assets. base->lib->base_assets->img.png.
then in dependencies of the other project:
dependencies: base: path: ../base
then to use the image:
Image.asset( "lib/base_assets/img.png", package: "base", )
I will note, I am declaring the use of the image in the base project and not the project that imports base because it defines the view. But it made the images work for android without adding them to the project that imports base.
This works as expected on iOS & macOS, but it fails on android
& web
(both renderers).
Tested with the code sample in https://github.com/flutter/flutter/issues/68983#issuecomment-716493116
The image also did not load after a hot reload like it was reported to in the past.
Updating labels
══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
The following Event object was thrown resolving an image frame:
[object Event]
When the exception was thrown, this was the stack
Image provider: AssetImage(bundle: null, name: "../images/dash.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#ce548(), name: "../images/dash.png",
scale: 1)
════════════════════════════════════════════════════════════════════════════════════════════════════
I found a workaround for people with an issue here. You can actually create a separate project to house the images and then import that project to use the assets. base->lib->base_assets->img.png. then in dependencies of the other project:
dependencies: base: path: ../base
then to use the image:Image.asset( "lib/base_assets/img.png", package: "base", )
I will note, I am declaring the use of the image in the base project and not the project that imports base because it defines the view. But it made the images work for android without adding them to the project that imports base.
This works.
Upgraded to P2 since this is something we either should 1) provide a recommended alternative workflow for or at least 2) explicitly document as unsupported.
Steps to Reproduce
If I try to use assets from a folder that's not within the project directory, the android app throws an error 'Unable to load asset' the first time that the app loads up. After a hot restart, the problem goes away.
Pubspec.yaml:
The app doesn't throw an error if I build it on iOS.
This seems to be a very weird bug, and I've tried everything from renaming every single thing, to running flutter clean dozens of times, to googling every possible solution.
Simply putting the assets folder inside the project directory seems to fix the error, but I need the assets to be outside of the project directory so I can access those assets for different applications.
Any help would be greatly appreciated!
Flutter (Channel master, 1.24.0-4.0.pre.55, on Mac OS X 10.15.2 19C57 darwin-x64)