flutter 带有缓存网络图像的CircleAvatar

ua4mk5z4  于 2023-06-30  发布在  Flutter
关注(0)|答案(1)|浏览(195)

我试图使用CachedNetworkImage与CircleAvatar,但无法。我得到下面的错误

SuchMethodError (NoSuchMethodError: No constructor '' declared in class 'null'.
Receiver: null
Tried calling: new ())

下面是我的代码相同。

Widget build(BuildContext context) {
    if (initials.length == 1) {
      initials = initials + ' ';
    }
    return CachedNetworkImage(
      placeholder: (context, url) => CircularProgressIndicator(),
      errorWidget: (context, url, error) => new Icon(Icons.error),
      fit: BoxFit.contain,
      imageUrl: user.profilePic ?? '',
      imageBuilder: (context, imageProvider) {
        // you can access to imageProvider
        return CircleAvatar(
            radius: radius,
            child: (initials != null)
                ? Center(
                    child: Text(initials,
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: radius * 2 / initials.length - 10,
                        )),
                  )
                : null,
            foregroundImage: imageProvider,
            backgroundColor:
                backgroundColor ?? UtilityFunctions.getRandomColor());
      },
    );
  }
uplii1fm

uplii1fm1#

尝试使用SizedBox.shrink()代替null

child: (initials != null)
                ? Center(
                    child: Text(initials,
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: radius * 2 / initials.length - 10,
                        )),
                  )
                : SizedBox.shrink(), //here

相关问题