FlutterGen / flutter_gen

The Flutter code generator for your assets, fonts, colors, … — Get rid of all String-based APIs.
https://pub.dev/packages/flutter_gen
MIT License
1.45k stars 144 forks source link

[FR]: Asset path as const #419

Open Nico04 opened 11 months ago

Nico04 commented 11 months ago

Is there an existing issue for this?

Describe the problem

I have several const that defines some static data in my app. Before using flutter_gen, I could write something like this : const breath = MultiSelectorItem('breath', 'assets/breath.svg', 'Breath');

But when using flutter_gen, this line doesn't work anymore : const breath = MultiSelectorItem('breath', Assets.breath.path, 'Breath');

Describe the solution

It seems that it's because the generated class SvgGenImage.path is a getter, so NOT const.

class SvgGenImage {
  const SvgGenImage(this._assetName);

  final String _assetName;

  [...]

  String get path => _assetName;
  String get keyName => _assetName;
}

Do you have an idea how can I use the asset path in a const ?

Additional context

I now I could remove flutter_svg: true in pubspec, but using this kind of code is also handy : child: Assets.breath.svg()

Request looks similar to #351, but it is somewhat different.

Code of Conduct

myxzlpltk commented 2 months ago

Even though this looks good. If its related to data or state, I think you should separate UI and logic. Create enum instead, and use switch from that.

child: switch(enumValue) {
    SomeEnum.Type1: Assets.breath.svg(),
    ....
}