无法在警报对话框内容中加载资产

问题描述

我知道有很多与此问题相关的线程,但它们没有帮助。 我有一个包含图像的资产文件夹。图像在代码的其他部分正确显示,但不在警报对话框中。在示例中,我使用了 .gif 文件,但我也遇到了与 .png 相同的问题。

来自调试控制台的错误

════════ 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/

我已经尝试使用 /assets/check.gif 但我遇到了同样的问题。

代码

 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,),],));
                                                  });

输出

My AlertDialog

希望你能帮我

解决方法

试试这个,我想你忘记在Widget中添加“assets”

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

您需要添加您的 folder path。就像您store your assets 所在的文件夹路径和您的资产名称一样。如下图

             Image.asset(
              'assets/check.gif',fit: BoxFit
                  .contain,

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...