flutter_gen
flutter_gen copied to clipboard
[FR]: Generate static const Asset Paths for Use in Constant Lists
Is there an existing issue for this?
- [X] I have searched the existing issues
Describe the problem
I’m currently using the flutter_gen package for asset management in my Flutter project. The package generates asset paths using getters, like this:
class Assets {
static final vectors = _Vectors();
}
class _Vectors {
String get homeFill => 'assets/vectors/home_fill.svg';
}
However, I want to use asset paths in Dart const lists, but since getters are evaluated at runtime, this isn’t possible. For instance:
const bottomNavs = [
{
"label": "Home",
"activeIcon": Assets.vectors.homeFill,
}
];
Since Assets.vectors.homeFill is a getter, it cannot be used in a const context, limiting its usability and efficiency in compile-time constant contexts.
Describe the solution
I propose adding an option to the flutter_gen package to generate asset paths as static const variables. This would allow asset paths to be used directly in const lists, improving performance and code clarity. For example:
class Assets {
static const String homeFill = 'assets/vectors/home_fill.svg';
}
class Assets {
static class Vectors {
static const String homeFill = 'assets/vectors/home_fill.svg';
}
}
This change would enable asset paths to be utilized in const contexts, making the code more efficient and easier to maintain.
Additional context
Benefits:
-
Performance: static const variables are evaluated at compile-time, which can enhance performance by reducing runtime overhead.
-
Immutability: Using const ensures that values are fixed and immutable, adhering to Dart’s best practices.
-
Code Clarity: Direct access to static const values improves code readability and predictability.
-
Configuration: Implement this feature as an optional configuration setting in
flutter_gento let developers choose between the current getter-based approach and the proposed static const method. -
Fallback: Keep the existing getter-based method for those who prefer or need the current implementation.
Thank you for considering this enhancement. It would significantly benefit developers who need to use asset paths in constant lists and improve overall code efficiency.
Code of Conduct
- [X] I agree to follow this project's Code of Conduct