无法为参数类型 'Widget Function(BuildContext, Widget, ImageChunkEvent)' 赋值 flutter dart

Argument type 'Widget Function(BuildContext, Widget, ImageChunkEvent)' can't be assigned flutter dart

我遇到以下错误 The argument type 'Widget Function(BuildContext, Widget, ImageChunkEvent)' can't be assigned to the parameter type 'Widget Function(BuildContext, Widget, ImageChunkEvent?)?'. 我正在尝试将 CircularProgressIndicator 添加到 Image.network 以在图像加载前显示。下面是我如何实现它


Padding(
              padding: const EdgeInsets.all(5),
              child: Builder(builder: (BuildContext context) {
                if (Config().equalsIgnoreCase(
                    "imageNetwork", widget.imageFetchType)) {
                  return CircleAvatar(
                      radius: 100,
                      child: ClipOval(
                        child: Image.network(
                          widget.picture,
                          width: 190,
                          height: 190,
                          fit: BoxFit.cover,
                          loadingBuilder: (BuildContext context, Widget child,
                          ImageChunkEvent loadingProgress){
                            if (loadingProgress == null) return child;
                            return Center(
                              child: CircularProgressIndicator(
                                value: loadingProgress.expectedTotalBytes != null
                                ? loadingProgress.cumulativeBytesLoaded /
                                loadingProgress.expectedTotalBytes
                                : null,
                              ),
                            )
                          },
                        ),
                      ),
                  // circle avatar background color
                  backgroundColor: Colors.deepOrange,);
                }

                return CircleAvatar(
                  radius: 100,
                  backgroundImage: NetworkImage(widget.picture),
                );

              }),
            )

错误出现在以下代码行


loadingBuilder: (BuildContext context, Widget child,
                          ImageChunkEvent loadingProgress){
                            if (loadingProgress == null) return child;
                            return Center(
                              child: CircularProgressIndicator(
                                value: loadingProgress.expectedTotalBytes != null
                                ? loadingProgress.cumulativeBytesLoaded /
                                loadingProgress.expectedTotalBytes
                                : null,
                              ),
                            )
                          }

我已尝试执行以下操作,但仍然出现错误

child: (_) => Builder(builder: (BuildContext context)

ImageChunkEvent 应该可以为空,因此使用 ImageChunkEvent?.

您还应该在 Center

之后添加一个 semi-colon

像这样:

   loadingBuilder: (BuildContext context, Widget child, ImageChunkEvent? loadingProgress) {
        if (loadingProgress == null) return child;
        return Center(
          child: CircularProgressIndicator(
            value: loadingProgress.expectedTotalBytes != null
                ? loadingProgress.cumulativeBytesLoaded /
                    loadingProgress.expectedTotalBytes!
                : null,
          ),
        );
      },