53

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.

JoergP
  • 1,349
  • 2
  • 13
  • 28

8 Answers8

101

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

croxx5f
  • 5,163
  • 2
  • 15
  • 36
  • 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.from(object) method for example, as it gives a similar error. Instead, you need to cast the response, as Map. – The Orchestrator Mar 23 '21 at 13:29
  • 2
    Whatever, thanks a lot for the workaround <3, it works like charm! – An Android Jun 30 '21 at 12:51
30

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 
),
Amir
  • 2,225
  • 2
  • 18
  • 27
17

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")
glittershark
  • 534
  • 2
  • 6
  • 19
Phor Joon Huang
  • 171
  • 1
  • 4
3

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,
    ),
  ),
)
LazzyCoderr
  • 81
  • 1
  • 2
2

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

For example:

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

as ImageProvider,

  • 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 '22 at 07:46
2

If you are using Image widget this will solve it:

imageObj!.image
halfer
  • 19,824
  • 17
  • 99
  • 186
Azizur Rahaman
  • 181
  • 2
  • 5
0

A simple workaround you could make in the meantime :

   decoration: BoxDecoration(
          image:const DecorationImage(
          image: AssetImage('assets/images/any.png'),
          fit: BoxFit.fitWidth),
          color: MyColors.yellow,
          borderRadius: 
          BorderRadius.circular(10)
          ),
Esmaeil Ahmadipour
  • 840
  • 1
  • 11
  • 32
-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