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#229296(:"package:flatt/src/painting/network_image_io.dart":断言失败:第22行位置14:"url!=null":不为真。
代码似乎不完整。如果错误出现过一次,但URL中的图像仍然显示在小部件上,那么小部件很可能试图从尚未初始化的url
加载图像。可能在屏幕的第一次渲染期间,url仍然为空,并且在重新加载时(即通过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(),
),
)