freezed icon indicating copy to clipboard operation
freezed copied to clipboard

Constant reference as default

Open aloisdeniel opened this issue 4 years ago • 2 comments

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,
     // ..
  });
}

aloisdeniel avatar Mar 25 '20 05:03 aloisdeniel

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)

rrousselGit avatar Mar 25 '20 05:03 rrousselGit

Should we mention / link this workaround in the What about @JsonKey annotation? section of the README.md? I think this would be helpful.

nilsreichardt avatar Feb 16 '22 11:02 nilsreichardt