即使我进行了空检查,Flutter 仍在加载网络图像
Flutter still loading network image even though i have a null check
在我的 flutter 应用程序中,我将 URLs 从 Firebase 存储存储在 Firestore 中。我希望 URL 为空,因为它取决于用户上传图片。在我的代码中,我正在检查空值,但出于某种原因,代码仍会尝试加载空值。如何正确防止颤动加载网络图像中的空值。我的加载代码如下:
Container(
width: 150,
height: 150,
child: ListView.builder(
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.all(8),
itemCount: photoList.length,
itemBuilder: (BuildContext context, int index) {
print(photoList[index]);
return photoList[index] != null ? Image.network(photoList[index]) : Container(height:400,child: Text('No Images to Display'));
}
),
),
当没有图像时,调试为上面的打印提供以下内容:
I/flutter (27038): url - null
当有图片时,就会有合适的 URL 并且图片加载没有任何问题。
所以不太确定为什么 Flutter 仍在尝试加载 null,即使在 null 的情况下我想要一个文本框来代替
photoList
中存储什么类型的数据?如果 String
有可能 "null" 值实际上是一个字符串而不是 null
.
看来photoList[index]
的值不一致。当它预期包含空值时打印“url - null”,当它不为空时,它包含加载图像的实际 url。
您可以选择以下任一方式:
确保您的 null 图像实际上是 null 而不是“url - null”,并且 null 检查将起作用。
使用 errorBuilder property of the Image.network 小部件显示您的错误小部件,如下所示:
Image.network(photoList[index],
errorBuilder: (context, error, stackTrace) {
return Container(height:400,child: Text('No Images to Display'));
}
)
在我的 flutter 应用程序中,我将 URLs 从 Firebase 存储存储在 Firestore 中。我希望 URL 为空,因为它取决于用户上传图片。在我的代码中,我正在检查空值,但出于某种原因,代码仍会尝试加载空值。如何正确防止颤动加载网络图像中的空值。我的加载代码如下:
Container(
width: 150,
height: 150,
child: ListView.builder(
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.all(8),
itemCount: photoList.length,
itemBuilder: (BuildContext context, int index) {
print(photoList[index]);
return photoList[index] != null ? Image.network(photoList[index]) : Container(height:400,child: Text('No Images to Display'));
}
),
),
当没有图像时,调试为上面的打印提供以下内容:
I/flutter (27038): url - null
当有图片时,就会有合适的 URL 并且图片加载没有任何问题。
所以不太确定为什么 Flutter 仍在尝试加载 null,即使在 null 的情况下我想要一个文本框来代替
photoList
中存储什么类型的数据?如果 String
有可能 "null" 值实际上是一个字符串而不是 null
.
看来photoList[index]
的值不一致。当它预期包含空值时打印“url - null”,当它不为空时,它包含加载图像的实际 url。
您可以选择以下任一方式:
确保您的 null 图像实际上是 null 而不是“url - null”,并且 null 检查将起作用。
使用 errorBuilder property of the Image.network 小部件显示您的错误小部件,如下所示:
Image.network(photoList[index], errorBuilder: (context, error, stackTrace) { return Container(height:400,child: Text('No Images to Display')); } )