Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Should I store assets path in static variable?

For example, I want to get Image logo.gif

Image.asset('images/logo.gif')

Should I do it like above or create new file to store static constant like below?

In assets_path.dart

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class imagePath {
  static const imageFolder = 'assets/images/';
  static const logo = imageFolder + 'logo.gif';
  //...and others images path
}

When use

import './assets_path.dart';
Image.asset(imagePath.logo)

>Solution :

Yes!! Highly recommended as literal strings are easy to mispell. So if you have multiple occurrences of the same file, the chances to accidentally mess up increases, so just create a static const variable inside a class called something like AppAssets for all file paths of all assets used in the application.
I do this for my apps. Also nothing is preventing you from doing the same for colors and styles, like AppColors and AppStyles

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading