首页 > 解决方案 > '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':不正确。

标签: flutter

解决方案


代码似乎不完整。如果错误出现一次,但来自 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(),
  ),
)

推荐阅读