Home > database >  Should I store assets path in static variable?
Should I store assets path in static variable?

Time:03-30

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

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)

CodePudding user response:

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

CodePudding user response:

If you need that asset path in several places, then yes, it is a good idea to store that information in one place and use the symbolic name wherever needed.

Certain types of changes to the application then become much easier and safer.

I consider it generally a good idea to replace literal constants with a meaningful name.

  • Related