I'm trying to define some assets for my Flutter app.
This is my directory structure:
- lib
- assets
- images
└ bg_login.png <-- this one is 400x800px
└ 2.0x
└ bg_login.png <-- this one is 800x1600px.
- test
- ios
- android
- build
- pubspec.yaml
This is my pubspec file (indented with 2 whitespaces):
name: my_app
description: My App
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.0
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
assets:
- assets/images/bg_login.png
I load the image like that:
new Positioned(
top: 0.0,
width: MediaQuery.of(context).size.width,
child: Image.asset(
"assets/images/bg_login.png",
fit: BoxFit.fitWidth,
)
)
Sometimes the image loads, sometimes it fails with this error:
Launching lib\main.dart on Android SDK built for x86 in debug mode...
Initializing gradle...
Resolving dependencies...
Running 'gradlew assembleDebug'...
Built build\app\outputs\apk\debug\app-debug.apk.
Installing build\app\outputs\apk\app.apk...
Syncing files to device Android SDK built for x86...
D/ ( 3460): HostConnection::get() New Host Connection established 0xb099df40, tid 3479
D/EGL_emulation( 3460): eglMakeCurrent: 0xa325a620: ver 2 0 (tinfo 0xb0983620)
I/flutter ( 3460): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
I/flutter ( 3460): The following assertion was thrown resolving an image codec:
I/flutter ( 3460): Unable to load asset: assets/images/bg_login.png
I/flutter ( 3460):
I/flutter ( 3460): When the exception was thrown, this was the stack:
I/flutter ( 3460): #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
I/flutter ( 3460): <asynchronous suspension>
I/flutter ( 3460): #1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:427:44)
I/flutter ( 3460): <asynchronous suspension>
I/flutter ( 3460): #2 AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:412:14)
I/flutter ( 3460): #3 ImageProvider.resolve.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:266:86)
I/flutter ( 3460): #4 ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:143:20)
I/flutter ( 3460): #5 ImageProvider.resolve.<anonymous closure> (package:flutter/src/painting/image_provider.dart:266:63)
I/flutter ( 3460): (elided 8 frames from package dart:async)
I/flutter ( 3460):
I/flutter ( 3460): Image provider: AssetImage(bundle: PlatformAssetBundle#267c3(), name: "assets/images/bg_login.png")
I/flutter ( 3460): Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#267c3(), name: "assets/images/bg_login.png",
I/flutter ( 3460): scale: 1.0)
I/flutter ( 3460): ════════════════════════════════════════════════════════════════════════════════════════════════════
It really happens randomly, sometimes it works, most of the time it throws that error. I also tried with ImageAsset
, I got the same error.
What's going on? Am I missing something on how to properly declare and load images?
2.0x
directory because I thought flutter was failing to load the image due to its size. But even managing different sizes didn't help. – Cossassets/img/
andassets/images/
discrepancy about between your explanation and the error message and the code? – Forgery