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

Instantiated initial value if null #601

Closed
lhengl opened this issue Feb 12, 2022 · 1 comment
Closed

Instantiated initial value if null #601

lhengl opened this issue Feb 12, 2022 · 1 comment
Labels
enhancement New feature or request needs triage

Comments

@lhengl
Copy link

lhengl commented Feb 12, 2022

Is your feature request related to a problem? Please describe.
I'm trying to implement IsarLinks with Freezed. This requires a field to be instantiated and initialised during the construction, which is not allowed by Freezed.

Currently the only way to define a default value is by using the @default(literal) annotation. I've searched through issues around this which dated back to 2 years - #64, #82, #149. These issues imply that the problem is gated by JsonSerializable. Which it is, if we are looking at it from the perspective of "default" values.

Describe the solution you'd like

What about an initial value? Why not add an @initial(value) annotation?

So the generated code would then become something like this:

Constructor({Class? field}) : field = field ?? intialValue, super._();

instead of this:

Constructor({this.field = defaultValue}) : super._();

Describe alternatives you've considered

I tried using this hack by by adding a JsonKey() to the field. But this doesn't work because we are still working in the realm of Default value which only accepts const literal.

Additional context

This problem is not related to JsonSerializable as far as I'm concerned because I've been able to address this issue by using a combination of packages to replace Freezed - Equatable, CopyWithExtension, and JsonSerialiazable. Not ideal because of the convenience of Freezed having all the features in one package. However, I've come up with this problem too many times needing an initial value which Freezed does not support.

@lhengl lhengl added enhancement New feature or request needs triage labels Feb 12, 2022
@rrousselGit
Copy link
Owner

Closing in favor of #64

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs triage
Projects
None yet
Development

No branches or pull requests

2 participants