Flutter-Asset Image

In Flutter, you can use asset images to include images in your app. Asset images are typically used for icons, images, and other static resources that your app needs.

Steps to Add an Image:

1)Create a new folder in the root of your flutter project. You can name it whatever you want, but assets are preferred. Inside this folder, you can create a subfolder called images to store your image assets.

2) Place the image you want to use in the assets/images folder or in the assets folder directly if you didn’t create a subfolder. The path should look like assets/images/yourImage. 

3)Open your project’s pubspec.yaml  file and edit it.

4)To add images, write the following code:

flutter:   
assets:
- assets/images/yourFirstImage.jpg
- assets/image/yourSecondImage.jpg

 If you want to include all the images of the assets folder then add this:

  flutter:               
    assets:     
           - assets/images/

Save the pubspec.yaml file after making the changes.

5) Insert the image code in the file, where you want to add the image.

     Image.asset('assets/images/yourFirstImage.jpg')
  

6) You can run your Flutter app on a simulator or a physical device to see the image displayed            in your app.
If your app is already running, you can use the hot reload feature to see changes without restarting the app.

Leave a comment

Your email address will not be published. Required fields are marked *