43

I just updated to Dart2 and Flutter sdk: '>=2.12.0 <3.0.0' and now this if statement breaks:

 decoration: new BoxDecoration(
              shape: BoxShape.circle,
              color: Colors.blueAccent,
              border: Border.all(
                  color: Colors.blueAccent,
                  width: 20.0,
                  style: BorderStyle.solid),
              image: new DecorationImage(
                fit: BoxFit.cover,
                image: myMarkerThumb != 'noImage'
                    ? NetworkImage(myMarkerThumb)
                    : AssetImage('assets/images/noImageAvailable.png'),
              ),
            ),

The argument type 'Object' can't be assigned to the parameter type 'ImageProvider'. ),

enter image description here

I'm just starting with flutter and have no idea where to look else.

7 Answers 7

85

Hey this is currently an issue I opened in the flutter repo with dart 2.12.

A simple workaround you could make in the meantime is just to cast the object.


 decoration:  BoxDecoration(
              shape: BoxShape.circle,
              color: Colors.blueAccent,
              border: Border.all(
                  color: Colors.blueAccent,
                  width: 20.0,
                  style: BorderStyle.solid),
              image:  DecorationImage(
                fit: BoxFit.cover,
                image: myMarkerThumb != 'noImage'
                    ? NetworkImage(myMarkerThumb)
                    : AssetImage('assets/images/noImageAvailable.png') as ImageProvider,
              ),
            ),

2
  • 1
    I have also experienced the same with the Navigator when assigning it to a variable and popping context with a Map. This Map cannot be decoded with the normal Map<String, String>.from(object) method for example, as it gives a similar error. Instead, you need to cast the response, as Map. Mar 23, 2021 at 13:29
  • 2
    Whatever, thanks a lot for the workaround <3, it works like charm!
    – An Android
    Jun 30, 2021 at 12:51
27

some one in opened issue solve the problem with casting to image provider

@AbdurrahmanElrayes

and this solution also work for me

image: DecorationImage( 
   image: true ? NetworkImage('someNetWorkLocation.com') : AssetImage('assets/images/noImageAvailable.png') as ImageProvider 
),
3
  • it will also solve in case of FileImage. for eg showing a image picked by imagePicker , we can show it with cast as Image provider
    – A.K.J.94
    Aug 1, 2021 at 7:51
  • Yeah, it's worked for me. just add the "as ImageProvider" end of the AssetImage. Aug 27, 2021 at 9:54
  • 3
    Great, you saved my time Dec 30, 2021 at 8:43
15

For those who used Image.file instead of NetworkImage, the solution should be like below

image: (imageFile != null) ? FileImage(imageFile!) as ImageProvider : AssetImage("assets/xxx.png")
0
2

Use Image.asset("URL").image to avoid static type casting "as ImageProvider". In the Future, maybe static type cast cause some errors.

Container(
  decoration: BoxDecoration(
    shape: BoxShape.circle,
    color: Colors.blueAccent,
    border: Border.all(color: Colors.blueAccent, width: 20.0, style: BorderStyle.solid),
    image: DecorationImage(
      fit: BoxFit.cover,
      image: myMarkerThumb != 'noImage' ?
      NetworkImage(myMarkerThumb) : 
      Image.asset('assets/images/noImageAvailable.png').image,
    ),
  ),
)
1

Use "as ImageProvider" after the end of the statement.

For example:

image: myMarkerThumb != 'noImage'
                    ? NetworkImage(myMarkerThumb)
                    : AssetImage('assets/images/noImageAvailable.png')

as ImageProvider,

1
  • Use code formatting for readability. Wrap your code block ``` or use the code button in the editor after selecting your code block.
    – Mudassir
    Jun 25, 2022 at 7:46
1

If you are using Image widget this will solve it:

imageObj!.image
-5

make sure that you do like this in the pubspec.yaml file

uses-material-design: true

assets: - assets/images/

because in the default settings the above code will be commented and it will add an extra path for specific images but the above given code will load all images in the assets/images/ folder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.