You are using assets in flutter app the wrong way

Learn how to use and manage assets easily and without hassle.


If you are a developer, you know what I am talking about. Assets are any fonts, icons, images, or files that are used inside the app. When the app gets large and assets will increase which comes with the challenge to get maintain the assets. In this article, we will take a look, at how can we manage and use assets most easily!

The Method,

  1. Get the necessary packages,

    1. Flutter Gen,

    2. Flutter Gen Runner,

    3. Build Runner,

  2. Go to pubspec.yaml file, and provide the path of your assets.

    Here, I have two folders they are icons, and images, which are responsible for storing the respective files.

  3. Import your assets into your app.

  1. Open your terminal, and run, dart pub global activate flutter_gen .This will initialize the package.

  2. Run, fluttergen -c pubspec.yaml .This will generate assets constants inside gen folder.

    When you open, the assets.gen.dart file. You can see the generated constants.

This is a part of the file but you can take a look at your file.

  1. Using the assets,

    1. You can use svg files like this. Assets class, will be generated and all the assets constants, are also generated using the file, and folder names. Here, .icons indicated the nested folder icons, and notificationIcon indicates notification_icon.svg .

    2. For using the png, and jpegs, you need to provide additional .keyName.

      I hope you enjoyed this article. For more, content like this visit Flutter World If you found any mistakes or even better solutions than this, please comment below. So that it can help the community.