The argument type 'Object' can't be assigned to the parameter type 'ImageProvider<Object>'

Viewed 33077

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.

5 Answers

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

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

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

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

For example:

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

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

Related