flutter - 'package:flutter/src/painting/_network_image_io.dart':断言失败:第 22 行 pos 14:'url != null':不正确
问题描述
class CustomCircleAvatar extends StatefulWidget {
final Image myImage;
final String initials;
CustomCircleAvatar({this.myImage, this.initials});
@override
_CustomCircleAvatarState createState() => new _CustomCircleAvatarState();
}
class _CustomCircleAvatarState extends State {
bool _checkLoading = true;
@override
void initState() {
super.initState();
widget.myImage.image.resolve(new ImageConfiguration()).addListener(
ImageStreamListener((ImageInfo info, bool synchronousCall) {
if (mounted) {
setState(() {
_checkLoading = false;
});
}
}));
}
@override
Widget build(BuildContext context) {
return _checkLoading == true
? new CircleAvatar(
child: new Text(
widget.initials,
style: TextStyle(fontSize: 60),
))
: new CircleAvatar(
backgroundImage: widget.myImage.image,
);
}
}
Positioned _profilePhoto(BuildContext context) {
return Positioned(
bottom: -70,
child: Container(
width: 150.0,
height: 150.0,
padding: EdgeInsets.all(3.0),
decoration: BoxDecoration(color: Colors.white, shape: BoxShape.circle),
child: CustomCircleAvatar(
myImage: Image.network(sellerPicture), // This sellerPicture i got from sharedPreferences
initials: '$sellerName'.substring(0, 1).toUpperCase(),
),
),
);
}
帮帮我,图像从 URL 显示,但终端说 URL != null 不正确════════ 小部件库捕获的异常═════════════════ ══════════════════════════════════════创建 ProfileScreen 时抛出以下断言: _ProfileScreenState#29296):'package:flutter/src/painting/_network_image_io.dart':断言失败:第 22 行 pos 14:'url != null':不正确。
解决方案
代码似乎不完整。如果错误出现一次,但来自 URL 的图像仍显示在小部件上,则小部件可能会尝试加载url
尚未初始化的图像。可能在第一次渲染屏幕期间,url 仍然是空的,并且在重新加载(即 via setState()
)时,url 已被初始化。此问题的解决方法是在参数中设置默认图像 url。
或添加一个检查器sellerPicture
Container(
/// Check if sellerPicture is null
child: sellerPicture != null ?
CustomCircleAvatar(
myImage: Image.network(sellerPicture), // This sellerPicture i got from
sharedPreferences
initials: '$sellerName'.substring(0, 1).toUpperCase(),
),
/// if sellerPicture is null, display a default image Widget
/// i.e. image from local Assets
: DefaultImageWidget(),
),
)
推荐阅读
- r - 使用 if 语句对字符串变量中的值进行子集化
- database - 创建连续聚合时 Azure Postgres 上的 TimescaleDB 错误
- javascript - 将逻辑移至 vuex 商店中的“正常”功能。辅助功能?
- c - 将 CFData 转换为 C 字符串
- javascript - reactjs的UI中未呈现成功消息
- elasticsearch - yaml文件中的logstash调度程序
- javascript - 如何检测本机反应中的亮度增加
- c - 使用 eMMC 作为 RingBuffer 磁盘
- visual-studio-code - 开发容器 JSON - 在 Insiders 1.60.0 中折旧的图像标签?
- node.js - 通过 id 获取用户的问题