flutter - Flutter - 'package:cached_network_image/src/image_provider/_image_provider_io.dart':断言失败:第 20 行 pos 16:'url != null':不正确
问题描述
CachedNetworkImageProvider 需要传递一个非 Null URL。我想做的是:当 _singleCategoryImage 为空时,只需将框着色为默认值,否则显示图像,但无法真正解决。
我收到这个错误。
'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)
),
);
}));
}
解决方案
您可以添加一个三元运算符来评估是否使用图像
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)))
推荐阅读
- java - 在包“android”中找不到属性“startcolor”的资源标识符
- scala - 无法使用已声明的函数
- javascript - hapijs - 在插件完成注册之前无法启动服务器
- sql - 如何查询(T-SQL)一列水平?
- sql - 基于一个属性的不同重复行
- r - 使用 geom_text_repel 保持顺序
- sql-server - 如何加载具有超过 10000 个字符的列的表?
- c# - 使用 EPPlus 库使用 C# 在 FTP 上直接创建 Excel 文件
- excel - vb.net 替换方法隐藏警告
- php - 当我尝试重定向到 laravel 5.6 中的另一个页面时,出现类似“抱歉,找不到您要查找的页面”之类的错误。