How to add asset image in flutter?

2.2k Views Asked by At

To add images from asset folder, follow this simple steps:

  1. create a folder with name of assets(you can give any name, but assets preferable).
  2. Register your asset folder and its images in pubspec.yaml.
  3. Then write this code to show images from assets folder:
 Image(
          image: AssetImage(
            'assets/car.jpg',
          ),
          fit: BoxFit.fitHeight,
        ),
2

There are 2 best solutions below

0
Braden Bagby On
  1. Do you have a file 'assets/car.jpg' in the root of your project? Thats a file 'car.jpg' under a folder 'assets'

  2. In your pubspec.yaml file (located at the root of your project), you must add reference to that folder. You should be able to find this comment and modify like below:

  # To add assets to your application, add an assets section, like this:
  assets:
      - assets/

The indentation is required. The forward slash also indicates its a folder and is required. If you are using VSCode some sort of .yaml formatter may help you get the correct indentation

  1. Your code looks good, so if you follow step 1 and 2 correctly you should be good to go
0
maryam hashemi On

To add images from the assets folder, follow these steps:

Create a folder named assets and put the photos you want in it. Register the assets folder in pubspec.yaml. As follows:::::::

assets:
  - assets/

Then write this code to display the images from the assets folder:

Image.asset('assets/orange.jpg'),