Search code examples
android-studioflutterdartassets

Troubleshooting Flutter Error: Unable to load asset


I've tried everything from every modification of the pubspec.yaml file, flutter clean, updating flutter and my IDE (Android Studio). Nothing seems to be working.

Building a money tracker app in the case that no transactions are on the page the image displays.

Path of image file:

lib/assets/images/waiting.png

Code:

return Container(
      height: 300,
      child: transactions.isEmpty
          ? Column(
              children: <Widget>[
                Text(
                  'No Transactions yet',
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 20.0,
                  ),
                ),
                Container(
                  height: 200,
                  child: Image.asset(
                    'assets/images/waiting.png',
                    fit: BoxFit.cover,
                  ),
                ),
              ],
            )
          : ListView.builder(
              itemBuilder: (context, index) {
                return Container(
                  margin: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
                  padding: EdgeInsets.all(3.0),
                  decoration: BoxDecoration(
                    gradient: LinearGradient(colors: _txColors),
                    border: Border.all(color: Colors.grey, width: 2.0),
                    borderRadius: BorderRadius.all(
                      Radius.circular(15.0),
                    ),
                    color: Colors.greenAccent,
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: <Widget>[
                      Container(
                        child: Text(
                          '\$${transactions[index].amount.toStringAsFixed(2)}',
                          style: TextStyle(
                            fontWeight: FontWeight.bold,
                            fontSize: 20.0,
                            color: Colors.white,
//                    Color(0xffC5CED4),
                          ),
                        ),
                      ),

Pubspec.yaml:

flutter:
uses-material-design: true
assets:
  - assets/images/

Error:

I/flutter ( 5679): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
I/flutter ( 5679): The following assertion was thrown resolving an image codec:
I/flutter ( 5679): Unable to load asset: assets/images/waiting.png
I/flutter ( 5679): 
I/flutter ( 5679): When the exception was thrown, this was the stack:
I/flutter ( 5679): #0      PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
I/flutter ( 5679): <asynchronous suspension>
I/flutter ( 5679): #1      AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:664:31)
I/flutter ( 5679): #2      AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:648:14)
I/flutter ( 5679): #3      ImageProvider.resolveStreamForKey.<anonymous closure> (package:flutter/src/painting/image_provider.dart:501:13)
I/flutter ( 5679): #4      ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:359:22)
I/flutter ( 5679): #5      ImageProvider.resolveStreamForKey (package:flutter/src/painting/image_provider.dart:499:80)
I/flutter ( 5679): #6      ScrollAwareImageProvider.resolveStreamForKey (package:flutter/src/widgets/scroll_aware_image_provider.dart:106:19)
I/flutter ( 5679): #7      ImageProvider.resolve.<anonymous closure> (package:flutter/src/painting/image_provider.dart:330:9)
I/flutter ( 5679): #8      ImageProvider._createErrorHandlerAndKey.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:460:26)
I/flutter ( 5679): (elided 13 frames from dart:async)
I/flutter ( 5679): 
I/flutter ( 5679): Image provider: AssetImage(bundle: null, name: "assets/images/waiting.png")
I/flutter ( 5679): Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#288b8(), name:
I/flutter ( 5679):   "assets/images/waiting.png", scale: 1.0)
I/flutter ( 5679): ════════════════════════════════════════════════════════════════════════════════════════════════════

Solution

  • I did have to move the asset folder to the same level of the lib folder. BUT I also had to fix indentation issues for the pubspec.yaml file.

    flutter:
      uses-material-design: true
      assets:
       - assets/
    --------------------------------
    flutter:
    [space][space]uses-material-design: true
    [space][space]assets:
    [space][space][space]- assets/