'package:flutter / src / painting / _network_image_io.dart':失败的断言:第22行pos 14:'url!= null':不正确

时间:2020-03-20 07:38:02

标签: flutter

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不正确 widget由小部件库捕获的异常═════════════════════════════════════ ══════════════════ 构建ProfileScreen(dirty,state:_ProfileScreenState#29296)时引发了以下断言: 'package:flutter / src / painting / _network_image_io.dart':断言失败:第22行pos 14:'url!= null':不正确。

1 个答案:

答案 0 :(得分:0)

代码似乎不完整。如果错误出现一次但来自 URL 的图像仍显示在小部件上,则小部件可能尝试从尚未初始化的 url 加载图像。可能在第一次渲染屏幕时,url 仍然是空的,并且在重新加载(即通过 setState())时,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(),
  ),
)