flutter_gen
flutter_gen copied to clipboard
Include package in AssetGenImage path property
Similiar to AssetImage's keyName property it would be helpful to change AssetGenImage's path property to include package so we can hide the package name inside of AssetGenImage:
- String get path => _assetName;
+ String get path =>
+ _package == null ? _assetName : 'packages/$_package/$_assetName';
_package could be generated, e.g.
final String? _package = 'example_resources';
without package_parameter_enabled: true:
final String? _package;
SvgGenImage could be changed accordingly.
Can you tell me your use case here?
so we can hide the package name inside of AssetGenImage
What's the benefit of this?
What's the benefit of this?
The reason is to encapsulate the package name so we can use the AssetGenImage without parameter, e.g.
use Image.asset(res.Assets.images.flutter3.path) instead of Image.asset(res.Assets.images.flutter3.path, package: 'example_resources')
(Just an example, we would use res.Assets.images.flutter3.image() instead of Image.asset(...) ).
In other cases there is no package parameter available. One use case is to load the data of the AssetGenImage directly and convert it into ui.Image which is used in our particle system, e.g.:
final imageData = await rootBundle.load(res.Assets.images.flutter3.path);
final uiImage = await decodeImageFromList(Uint8List.view(imageData.buffer));
On the Flame engine we don't use widgets. Having a generated path with the package name would be helpful. It was possible to get that before #229 (via .keyName property that AssetImage has).
@kolotum @renancaraujo
When package_parameter_enabled is true, Can you solve this problem by generating the following?
String get path => _assetName;
// THIS
String get keyName => 'packages/$packageName/\$_assetName'; // 'packages/example_resources/assets/images/flutter3.jpg'
or do you need always it? when package_parameter_enabled is false.
@kolotum @renancaraujo
When
package_parameter_enabledistrue,Can you solve this problem by generating the following?String get path => _assetName; // THIS String get keyName => 'packages/$packageName/\$_assetName'; // 'packages/example_resources/assets/images/flutter3.jpg'or do you need always it? when
package_parameter_enabledisfalse.
If you would like to keep the path value to not break the current implementations it is okay to introduce a new getter keyName.
I would still prefer a property where I do not need to now the value of package_parameter_enabled.
So I think of something like
String get keyName => _package == null ? _assetName : 'packages/$packageName/$_assetName';
or
// when package_parameter_enabled is false
String get keyName => _assetName;
// when package_parameter_enabled is true:
String get keyName => 'packages/$packageName/$_assetName';
@kolotum Thank you for your response. I'd like to keep the path variable, so I will do the following (a bit redundant).
When package_parameter_enabled = true
String get path => _assetName;
String get keyName => 'packages/$packageName/\$_assetName';
When package_parameter_enabled = false
String get path => _assetName;
String get keyName => _assetName;
@kolotum @renancaraujo I just released v4.3.0, Thank you all.