使用 FadeInImage 和 url [Flutter] 图像不能为空

Image cannot be null using FadeInImage and url [Flutter]

你好,所以我添加了一种在列表中显示我的 url 图片的方法,但是因为我是从 public API 获取此信息,其中一些缺少 URL 并且发生错误,我尝试使用占位符和图像 errorbuilder 但没有帮助

这是我的代码:

child: FadeInImage.assetNetwork(
                          height: 100,
                          width: 100,
                          fit: BoxFit.fill,
                          image: newsList[index].urlToImage,
                          placeholder: 'images/placeholder.png',
                          imageErrorBuilder: (context, error, StackTrace) {
                            return const Image(
                                height: 100,
                                width: 100,
                                image: AssetImage("images/placeholder.png"));
                          },
                        ),

和错误:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown building:
'package:flutter/src/widgets/fade_in_image.dart': Failed assertion: line 234 pos 15: 'image !=
null': is not true.

更新代码:

child: newsList[index].urlToImage == null
                            ? Container(
                                decoration: BoxDecoration(
                                  image: DecorationImage(
                                    image: AssetImage("images/placeholder.png"),
                                  ),
                                ),
                              )
                            : FadeInImage.assetNetwork(
                                height: 100,
                                width: 100,
                                fit: BoxFit.fill,
                                image: newsList[index].urlToImage,
                                placeholder: 'images/placeholder.png',
                                imageErrorBuilder:
                                    (context, error, StackTrace) {
                                  return const Image(
                                      height: 100,
                                      width: 100,
                                      image:
                                          AssetImage("images/placeholder.png"));
                                },
                              ),

和错误信息:

imageErrorBuilder只有在图片加载过程中出现错误时才会调用,例如图片路径不存在。

要解决您的问题,您必须检查 url 是否为空。如果为空,则显示另一个小部件,否则显示 FadeInImage.

newsList[index].urlToImage == null ? DisplayNoImage: FadeInImage.assetNetwork(...),