Search code examples
flutterdartflutter-circularprogressindicator

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


I am having the following error The argument type 'Widget Function(BuildContext, Widget, ImageChunkEvent)' can't be assigned to the parameter type 'Widget Function(BuildContext, Widget, ImageChunkEvent?)?'. I am trying to add a CircularProgressIndicator to Image.network that will show before image loads. Below is how i am implementing it


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),
                );

              }),
            )

The error comes in the following lines of code


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,
                              ),
                            )
                          }

I have tried doing the following below but its still having the error

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


Solution

  • ImageChunkEvent should be nullable hence use ImageChunkEvent?.

    You should also add a semi-colon after the Center

    Like so:

       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,
              ),
            );
          },