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

Unable to load asset in Alert Dialog content

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

调试控制台出错

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

输出:

希望你能帮助我

试试这个,我想你忘了在 Widget

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

您需要添加您的 folder path。比如你 store your assets 那个文件夹路径和你的资产名称。喜欢下面

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