Flutter 颤振';包:缓存的\u网络\u图像/src/image\u提供程序/\u图像\u提供程序\u io.dart&\x27;:断言失败:第20行第16位:';网址!=空';:事实并非如此

Flutter 颤振';包:缓存的\u网络\u图像/src/image\u提供程序/\u图像\u提供程序\u io.dart&\x27;:断言失败:第20行第16位:';网址!=空';:事实并非如此,flutter,dart,flutter-layout,Flutter,Dart,Flutter Layout,CachedNetworkImageProvider要求传递非空URL。 我想做的是:当_singleCategoryImage为空时,只需将框的颜色设置为默认值,否则将显示图像,但无法真正找到执行该操作的方法 我得到了这个错误 'package:cached_network_image/src/image_provider/_image_provider_io.dart': Failed assertion: line 20 pos 16: 'url != null': is not true

CachedNetworkImageProvider要求传递非空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)
              ),
            );
          }));
}

错误表明,您提供的用于从网络加载图像的URL为空,请尝试使用print语句检查URL

错误表明,您提供的用于从网络加载图像的URL为空,请尝试使用print语句检查URL

您可以添加一个三值运算符来评估是否使用图像

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

您可以添加一个三元运算符来计算是否使用图像

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