CachedNetworkImageProvider需要传递一个非null的URL。 我正在尝试做的是:当_singleCategoryImage为null时,仅将框设置为默认颜色,否则将显示图像,但实际上无法弄清楚。
我遇到此错误。
'package:cached_network_image/src/image_provider/_image_provider_io.dart': Failed assertion: line 20 pos 16: 'url != null': is not true.
来源:
Widget singleCategoryTemp(_singleCategoryText, _singleCategoryImage) {
return Card(
elevation: 0,
color: Colors.transparent,
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Container(
margin: (EdgeInsets.all(MediaQuery.of(context).size.width / 27)),
child: Center(
child: Text(
_singleCategoryText,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: MediaQuery.of(context).size.width / 17),
textAlign: TextAlign.center,
),
),
decoration:
new BoxDecoration(
image: DecorationImage(
image: CachedNetworkImageProvider(_singleCategoryImage),
/* image: NetworkImage(
_singleCategoryImage), */
fit: BoxFit.cover,
),
// gradient: LinearGradient(colors: [Colors.red, Colors.purple]),
borderRadius: new BorderRadius.circular(20.0),
color: Color(0xFF6d6e70)
),
);
}));
}
答案 0 :(得分:0)
该错误表明,您提供的用于从网络加载图像的URL为空,请尝试使用print语句检查该URL。
答案 1 :(得分:0)
您可以添加三元运算符来评估是否使用图片
decoration: _singleCategoryImage != null
? new BoxDecoration(
image: DecorationImage(
image: CachedNetworkImageProvider(_singleCategoryImage),
/* image: NetworkImage(
_singleCategoryImage), */
fit: BoxFit.cover,
),
// gradient: LinearGradient(colors: [Colors.red, Colors.purple]),
borderRadius: new BorderRadius.circular(20.0),
color: Color(0xFF6d6e70))
: new BoxDecoration(....), //<==== Your decoration without image
或者这另一个选择只是避免加载图像,而是使用相同的BoxDecoration
decoration: new BoxDecoration(
image: _singleCategoryImage != null
? DecorationImage(
image: CachedNetworkImageProvider(_singleCategoryImage),
/* image: NetworkImage(
_singleCategoryImage), */
fit: BoxFit.cover,
)
: null,
// gradient: LinearGradient(colors: [Colors.red, Colors.purple]),
borderRadius: new BorderRadius.circular(20.0),
color: Color(0xFF6d6e70)))