skip to Main Content

I am getting error in the image and error is:

The argument ‘Image’ type can’t be assigned to a parameter type ‘ImageProvider'<object>

Why this error is coming?

I have this code:

ListTile(
  title: Text('Manzar'),
  subtitle: Text('Where are you from'),
  trailing: Text('6:30 pm'),
  leading: CircleAvatar(
      `your text`,
       backgroundImage: Image.asset('assets/images/pic.jpg'),
    ),
);

3

Answers


  1. Do This :-

    const ListTile(
                    title: Text('Manzar'),
                    subtitle: Text('Where are you from'),
                    trailing: Text('6:30 pm'),
                    leading: CircleAvatar(
                      backgroundImage: AssetImage("YOUR_IMAGE"),
                    ),
                  )
    

    ImageProvider accept AssetImage as perameter

    Login or Signup to reply.
  2. You are passing an object of type Image to the field backgroundImage, which is expecting an objet of type ImageProvider.

    • Image is a widget class, which means it is a Flutter widget used to render images. This is what it is actually using CircleAvatar underneath to represent the background image.
    • ImageProvider is interface-class which is used to provide information to widgets on the data of an image (retrieved from a file, asset, network, …)

    In your case, you shall just provide your asset image as an ImageProvider of type AssetImage:

    ListTile(
      title: Text('Manzar'),
      subtitle: Text('Where are you from'),
      trailing: Text('6:30 pm'),
      leading: CircleAvatar(
           child: Text(`your text`),
           backgroundImage: AssetImage('assets/images/pic.jpg'),
        ),
    );
    
    Login or Signup to reply.
  3. The error you’re encountering is because the backgroundImage property of CircleAvatar expects an object of type ImageProvider, but you’re providing it with an Image widget directly. Fix this by this way:

    ListTile(
      title: Text('Manzar'),
      subtitle: Text('Where are you from'),
      trailing: Text('6:30 pm'),
      leading: CircleAvatar(
        backgroundImage: AssetImage('assets/images/pic.jpg'),
      ),
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search