flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
164.9k stars 27.16k forks source link

Unable to load asset from relative asset import outside of project directory on Android/web #68983

Open ammarhemani opened 3 years ago

ammarhemani commented 3 years ago

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:

flutter:
  assets:
    - ../assets/

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)

TahaTesser commented 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

ammarhemani commented 3 years ago

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

TahaTesser commented 3 years ago

@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

complete code sample ```dart import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Material App', theme: ThemeData.dark(), home: Home(), ); } } class Home extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Material App Bar'), ), body: Center( child: Image.asset('../images/dash.png'), ), floatingActionButton: FloatingActionButton( child: Icon(Icons.add), onPressed: () {}, ), ); } } ```
  assets:
    - ../images/
flutter doctor -v ```bash [✓] Flutter (Channel stable, 1.22.2, on Mac OS X 10.15.7 19H2, locale en-GB) • Flutter version 1.22.2 at /Users/tahatesser/Code/flutter_stable • Framework revision 84f3d28555 (10 days ago), 2020-10-15 16:26:19 -0700 • Engine revision b8752bbfff • Dart version 2.10.2 [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2) • Android SDK at /Users/tahatesser/Code/sdk • Platform android-30, build-tools 30.0.2 • ANDROID_HOME = /Users/tahatesser/Code/sdk • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 12.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 12.1, Build version 12A7403 • CocoaPods version 1.10.0.rc.1 [!] Android Studio (version 4.1) • Android Studio at /Applications/Android Studio.app/Contents ✗ Flutter plugin not installed; this adds Flutter specific functionality. ✗ Dart plugin not installed; this adds Dart specific functionality. • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593) [✓] VS Code (version 1.50.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.15.1 [✓] Connected device (1 available) • iPhone 12 (mobile) • 318957C3-E4BB-4B35-A3C7-14F515B0E665 • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-1 (simulator) ! Doctor found issues in 1 category. ```
talhakhan1297 commented 1 year ago

Any updates or workarounds?

johnny-liang-gd commented 1 year ago

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, 😭

parkerjensendev commented 1 year ago

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?

parkerjensendev commented 1 year ago

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.

danagbemava-nc commented 5 months ago

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)
════════════════════════════════════════════════════════════════════════════════════════════════════
flutter doctor -v ``` [✓] Flutter (Channel stable, 3.19.5, on macOS 14.3.1 23D60 darwin-arm64, locale en-GB) • Flutter version 3.19.5 on channel stable at /Users/nexus/dev/sdks/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 300451adae (7 days ago), 2024-03-27 21:54:07 -0500 • Engine revision e76c956498 • Dart version 3.3.3 • DevTools version 2.31.1 [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/nexus/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • Java binary at: /Users/nexus/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 15.3) • Xcode at /Applications/Xcode-15.3.0.app/Contents/Developer • Build 15E204a • CocoaPods version 1.14.3 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2023.1) • Android Studio at /Users/nexus/Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314) [✓] IntelliJ IDEA Ultimate Edition (version 2023.2.5) • IntelliJ at /Users/nexus/Applications/IntelliJ IDEA Ultimate.app • Flutter plugin version 77.2.2 • Dart plugin version 232.10286 [✓] VS Code (version 1.87.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.84.0 [✓] Connected device (5 available) • Pixel 7 (mobile) • 28291FDH2001SA • android-arm64 • Android 14 (API 34) • Nexus (mobile) • 00008020-001875E83A38002E • ios • iOS 17.4.1 21E236 • Dean’s iPad (mobile) • 00008103-000825C811E3401E • ios • iOS 17.4.1 21E236 • macOS (desktop) • macos • darwin-arm64 • macOS 14.3.1 23D60 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 123.0.6312.105 [✓] Network resources • All expected network resources are available. • No issues found! ``` ``` [!] Flutter (Channel master, 3.22.0-4.0.pre.33, on macOS 14.3.1 23D60 darwin-arm64, locale en-GB) • Flutter version 3.22.0-4.0.pre.33 on channel master at /Users/nexus/dev/sdks/flutters ! Warning: `flutter` on your path resolves to /Users/nexus/dev/sdks/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/nexus/dev/sdks/flutters. Consider adding /Users/nexus/dev/sdks/flutters/bin to the front of your path. ! Warning: `dart` on your path resolves to /Users/nexus/dev/sdks/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/nexus/dev/sdks/flutters. Consider adding /Users/nexus/dev/sdks/flutters/bin to the front of your path. • Upstream repository https://github.com/flutter/flutter.git • Framework revision 6a0573dbdd (5 hours ago), 2024-04-04 01:52:25 -0400 • Engine revision ca7596642c • Dart version 3.5.0 (build 3.5.0-18.0.dev) • DevTools version 2.34.1 • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades. [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/nexus/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • Java binary at: /Users/nexus/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 15.3) • Xcode at /Applications/Xcode-15.3.0.app/Contents/Developer • Build 15E204a • CocoaPods version 1.14.3 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2023.1) • Android Studio at /Users/nexus/Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314) [✓] IntelliJ IDEA Ultimate Edition (version 2023.2.5) • IntelliJ at /Users/nexus/Applications/IntelliJ IDEA Ultimate.app • Flutter plugin version 77.2.2 • Dart plugin version 232.10286 [✓] VS Code (version 1.87.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.84.0 [✓] Connected device (6 available) • Pixel 7 (mobile) • 28291FDH2001SA • android-arm64 • Android 14 (API 34) • Nexus (mobile) • 00008020-001875E83A38002E • ios • iOS 17.4.1 21E236 • Dean’s iPad (mobile) • 00008103-000825C811E3401E • ios • iOS 17.4.1 21E236 • macOS (desktop) • macos • darwin-arm64 • macOS 14.3.1 23D60 darwin-arm64 • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin • macOS 14.3.1 23D60 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 123.0.6312.105 [✓] Network resources • All expected network resources are available. ! Doctor found issues in 1 category. ```
talhakhan1297 commented 5 months ago

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.

andrewkolos commented 3 weeks ago

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.