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

8

64

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.

Maplemaples answered 10/3, 2021 at 8:42 Comment(0)
D
114

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

Dumond answered 12/3, 2021 at 10:34 Comment(2)
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.Behl
Whatever, thanks a lot for the workaround <3, it works like charm!Chisolm
E
32

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 
),
Employee answered 19/6, 2021 at 7:14 Comment(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 providerPullover
Yeah, it's worked for me. just add the "as ImageProvider" end of the AssetImage.Acceptant
Great, you saved my timeActionable
S
20

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")
Sculptor answered 5/4, 2022 at 14:19 Comment(0)
T
4

Use as ImageProvider after the end of the statement.

For example:

image: myMarkerThumb != 'noImage'
     ? NetworkImage(myMarkerThumb)
     : AssetImage('assets/images/noImageAvailable.png')
     as ImageProvider,
Trachytic answered 24/6, 2022 at 8:16 Comment(1)
Use code formatting for readability. Wrap your code block ``` or use the code button in the editor after selecting your code block.Valer
C
4

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,
    ),
  ),
)
Custommade answered 26/12, 2022 at 7:26 Comment(0)
J
3

If you are using Image widget this will solve it:

imageObj!.image
Jansenism answered 13/1, 2023 at 4:46 Comment(0)
U
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)
          ),
Unbodied answered 24/4, 2023 at 22:54 Comment(0)
M
-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

Miscreance answered 28/3, 2022 at 7:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.