Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Set backgroundImage of type ImageProvider<Object> with conditional statment in Flutter

I would like to set the backgroundImage property of a CircleAvatar with a conditional statement. I have a variable _imageProvided of type bool. If it is true a FileImage should be assigned to the backgroundImage property, else it should assign an AssetImage.

The Error that occurred: The argument type ‘Object’ can’t be assigned to the parameter type ‘ImageProvider<Object>?

The code Works when I assigned the a AssetImage or the FileImage separately.
Like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

CircleAvatar(
    .....
    backgroundImage: FileImage(MyFileVariable),
    .....
);

And Like this:

CircleAvatar(
    .....
    backgroundImage: AssetImage("MyAssetPath"),
    .....
);

But the error mentioned above occurred, when I tried this:

CircleAvatar(
    .....
    backgroundImage: _imageProvided ? FileImage(MyFileVariable) : AssetImage("MyAssetPath"),
    .....
);

>Solution :

You could cast it like this; FileImage(MyFileVariable) as ImageProvider? so the whole code block would be like this:

CircleAvatar(
    .....
    backgroundImage: _imageProvided ? FileImage(MyFileVariable) as ImageProvider?: AssetImage("MyAssetPath"),
    .....
);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading