1

我知道有很多与这个问题相关的线程,但它们没有帮助。我有一个包含图像的资产文件夹。图像在代码的其他部分正确显示,但不在警报对话框中。在示例中,我使用了 .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)

发布规范.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,
                                                              ),
                                                            ],
                                                          ),
                                                        ));
                                                  });

输出:

我的警报对话框

希望你能帮助我

4

2 回答 2

2

试试这个,我想你忘了在小部件中添加“资产”

 Image.asset('assets/check.gif', width: 20,height: 20,fit:BoxFit.contain,),
于 2021-01-29T12:51:51.593 回答
2

您需要添加您的folder path. 就像您store your assets的文件夹路径和资产名称一样。像下面这样

             Image.asset(
              'assets/check.gif',
              width: 20,
              height: 20,
              fit: BoxFit
                  .contain,
            ),
于 2021-01-29T12:52:27.153 回答