Ad

Unable To Load Asset In Alert Dialog Content

I know that there are a lot of thread related to this question but they didn't help. I have an asset folder containing images. Images are properly shown in other parts of the code but not in the Alert Dialog. In the example I use a .gif file, but I had the same problem also with .png.

Error from Debug Console

════════ Exception caught by image resource service ════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: check.gif

When the exception was thrown, this was the stack
#0      PlatformAssetBundle.load
package:flutter/…/services/asset_bundle.dart:225
<asynchronous suspension>
#1      AssetBundleImageProvider._loadAsync
package:flutter/…/painting/image_provider.dart:668
#2      AssetBundleImageProvider.load
package:flutter/…/painting/image_provider.dart:651
#3      ImageProvider.resolveStreamForKey.<anonymous closure>
package:flutter/…/painting/image_provider.dart:504
...
Image provider: AssetImage(bundle: null, name: "check.gif")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#234cb(), name: "check.gif", scale: 1.0)

pubspec.yaml

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

I already tried using /assets/check.gif but I had the same problem.

Code

 showDialog(
                                                  context: context,
                                                  builder:
                                                      (BuildContext context) {
                                                    return AlertDialog(
                                                        shape: RoundedRectangleBorder(
                                                            borderRadius:
                                                                BorderRadius
                                                                    .all(Radius
                                                                        .circular(
                                                                            13)),
                                                            side: BorderSide(
                                                                color: const Color(
                                                                    0xff5387ff),
                                                                width: 3)),
                                                        content: Container(
                                                          height: MediaQuery.of(
                                                                      context)
                                                                  .size
                                                                  .height /
                                                              9,
                                                          child: Column(
                                                            children: [
                                                              Image.asset(
                                                                'check.gif',
                                                                width: 20,
                                                                height: 20,
                                                                fit: BoxFit
                                                                    .contain,
                                                              ),
                                                            ],
                                                          ),
                                                        ));
                                                  });

Output:

My AlertDialog

Hope you'll be albe to help me

Ad

Answer

You need to add your folder path. Like where you store your assets that folder path and your asset name. Like the below

             Image.asset(
              'assets/check.gif',
              width: 20,
              height: 20,
              fit: BoxFit
                  .contain,
            ),
Ad
source: stackoverflow.com
Ad