freezed
freezed copied to clipboard
Constant reference as default
I doubt that there's something to do for this issue (the solution can help someone though) ... but if I use a local constant reference in a @Default
, it does not compile.
abstract class SleekButtonStyle {
static const defaultTransitionDuration = const Duration(milliseconds: 80);
factory SleekButtonStyle({
@Default(defaultTransitionDuration) Duration transitionDuration,
// ..
});
}
This results to :
class _$_SleekButtonStyle
with DiagnosticableTreeMixin
implements _SleekButtonStyle {
_$_SleekButtonStyle(
{this.transitionDuration = defaultTransitionDuration, /// <- Should be `SleekButtonStyle.defaultTransitionDuration`
// ...
Solution
You should put the explicit reference location.
abstract class SleekButtonStyle {
static const defaultTransitionDuration = const Duration(milliseconds: 80);
factory SleekButtonStyle({
@Default(SleekButtonStyle.defaultTransitionDuration) Duration transitionDuration,
// ..
});
}
Yeah, it's difficult.
I suppose we could search inside the source code for variable matching the name of the static variables and prefix them with the original class name.
Some cases could be complex though, like @Default(someStatic > 0)
Should we mention / link this workaround in the What about @JsonKey annotation?
section of the README.md
? I think this would be helpful.