Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FR]: Generate static const Asset Paths for Use in Constant Lists #544

Open
2 tasks done
Fingertips18 opened this issue Jul 25, 2024 · 0 comments
Open
2 tasks done
Labels
enhancement New feature or request

Comments

@Fingertips18
Copy link

Is there an existing issue for this?

  • 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_gen to 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

  • I agree to follow this project's Code of Conduct
@Fingertips18 Fingertips18 added the enhancement New feature or request label Jul 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant