In this article I will tell you step by step how to add an asset image in Flutter.
- First of all create a root level folder called “assets”, you can name it anything else but the general naming convention is to name it so.
- You can create subfolders in this folder but we are not going to do so just to keep the article simple.
- Now drag an example image (lets say “a.png”) to the assets folder.
- Now head over to the pubspec.yaml file and head over to the section commented right under the “uses-material-design: true” line.
- We can see an example asset is commented out.
- We can either delete or ignore that comment for advanced developers but we are going to use it in this article for beginners sake.
- Simply select the portion from “assets” upto the example image path.
- Now hit “Ctrl”+”Shift”+”/” to uncomment the lines.
- Now change the path as required, here we justy need to change path to “assets/a.png”.
- Keep the spacing as shown in the example comments, pubspec.yaml is format sensitive.
- Run “pubspec get” to load the image into the project.
- Now you can use it anywhere using Image.asset(“assets/a.png”).