Flutter_cached_network_image: using placeholder on CachedNetworkImageProvider or directly use CachedNetworkImage

Created on 14 May 2019  路  10Comments  路  Source: Baseflow/flutter_cached_network_image

when i try to use this library on Container to make circle image i get error

The argument type 'CachedNetworkImage' can't be assigned to the parameter type 'ImageProvider'.

code:

image: DecorationImage(
  image: CachedNetworkImage(
    imageUrl: "http://via.placeholder.com/350x150",
    placeholder: (context, url) => new CircularProgressIndicator(),
    errorWidget: (context, url, error) => Image.asset("assets/images/profile.jpg"),
  ),
),

now i try to use another way for example:

image: new DecorationImage(image:new CachedNetworkImageProvider(url)),

so how can i have placeholder and errorWidget in this implementation?

Most helpful comment

@MahdiPishguy sorry for my late reaction.

ImageProviders can only return 1 image, so this cannot return a placeholder first.
All image widgets on Android expect an imageprovider, so indeed you cannot use CachedNetworkImage directly. CachedNetworkImage does provide a callback which gives an imageprovider. What you can do is something like this (I'll improve the readme file to show this option):

CachedNetworkImage(
  imageUrl: "http://notAvalid.uri",
  imageBuilder: (context, imageProvider) => Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: imageProvider,
            fit: BoxFit.cover,
          ),
        ),
      ),
  placeholder: (context, url) => CircularProgressIndicator(),
  errorWidget: (context, url, error) => Icon(Icons.error),
);

All 10 comments

Did u find a solution to this?

@colt005 no, i can't

Use CachedNetworkImageProvider instead of CachedNetworkImage, It would solve the problem.

@colt005

in my issue as i said

now i try to use another way for example:
image: new DecorationImage(image:new CachedNetworkImageProvider(url)),
so how can i have placeholder and errorWidget in this implementation?

I used an if condition to check if the url is empty, it simply solved the issue in my use-case.

@MahdiPishguy sorry for my late reaction.

ImageProviders can only return 1 image, so this cannot return a placeholder first.
All image widgets on Android expect an imageprovider, so indeed you cannot use CachedNetworkImage directly. CachedNetworkImage does provide a callback which gives an imageprovider. What you can do is something like this (I'll improve the readme file to show this option):

CachedNetworkImage(
  imageUrl: "http://notAvalid.uri",
  imageBuilder: (context, imageProvider) => Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: imageProvider,
            fit: BoxFit.cover,
          ),
        ),
      ),
  placeholder: (context, url) => CircularProgressIndicator(),
  errorWidget: (context, url, error) => Icon(Icons.error),
);

@renefloor

thanks to reply, please let me to check that

I've also added it to the readme now.

i have the same issue, i want to use CachedNetworkImage on image property for the DecorationImage but it says:
The argument type 'CachedNetworkImage' can't be assigned to the parameter type 'ImageProvider'
Any help

If you want to use it with DecorationImage you need to use CachedNetworkImageProvider. If you want to combine it with a placeholder you can do like this:

CachedNetworkImage(
  imageUrl: "http://via.placeholder.com/200x150",
  imageBuilder: (context, imageProvider) => Container(
    decoration: BoxDecoration(
      image: DecorationImage(
          image: imageProvider,
          fit: BoxFit.cover,
          colorFilter:
              ColorFilter.mode(Colors.red, BlendMode.colorBurn)),
    ),
  ),
  placeholder: (context, url) => CircularProgressIndicator(),
  errorWidget: (context, url, error) => Icon(Icons.error),
),
Was this page helpful?
0 / 5 - 0 ratings

Related issues

BerndWessels picture BerndWessels  路  6Comments

love-bkpp picture love-bkpp  路  5Comments

gunhansancar picture gunhansancar  路  3Comments

chow2324 picture chow2324  路  4Comments

yossefEl picture yossefEl  路  4Comments