[Solved] The return type 'AssetImage' isn't a 'void', as required by the closure's context

Question

Asked by Petro on September 20, 2022 (source).

How can I add error detection for a Decorated Network image?

           decoration: BoxDecoration(
                  image: 
                  //NetworkImage but fallback to local image if http request fails:
                      DecorationImage(
                          image: NetworkImage(mediaUrl!),
                          fit: BoxFit.cover,
                          onError: (exception, stackTrace) {
                            return AssetImage('assets/images/placeholder.jpg');
                          }
                      )
              ),

enter image description here

Answer

Question answered by Petro (source).

I was able to resolve my issue and keep the circle decoration by wrapping the decoration inside of a CachedNetworkImage which already contains a good error catching method

  CachedNetworkImage(
                  imageUrl: userProfileImg!,
                  imageBuilder: (context, imageProvider) => Container(
                    width: 60.0,
                    height: 60.0,
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      image: DecorationImage(
                          image: imageProvider, fit: BoxFit.cover),
                    ),
                  ),
                  placeholder: (context, url) => new CircularProgressIndicator(backgroundColor: Colors.white,),
                  errorWidget: (context, exception, stackTrack) => Icon(Icons.person,color: Colors.grey,size: 35,),

              ),
DART FLUTTER
SHARE: