rrousselGit/freezed

Add option to include getters in toString() and exclude parameters

Aperta

#698 aperta il 6 lug 2022

 (1 commento) (0 reazioni) (1 assegnatario)Dart (300 fork)github user discovery
enhancementgood first issue

Metriche repository

Star
 (2177 stelle)
Metriche merge PR
 (Merge medio 25g 18h) (1 PR mergiata in 30 g)

Descrizione

Is your feature request related to a problem? Please describe. Sometimes it's helpful for debugging to have the result of some getters in the toString() method.

Describe the solution you'd like

// until now
@freezed
class MyClass with _$MyClass {
  const factory MyClass({
    required int a,
    required int b,
  }) = _MyClass;

  const MyClass._();

  int get sum => a + b;
}

MyClass(a: 40, b: 2).toString(); // => 'MyClass(a: 40, b: 2)'

// expected
MyClass(a: 40, b: 2).toString(); // => 'MyClass(a: 40, b: 2, sum: 42)'

// could be achieved by annotating the whole class or a single getter

@Freezed(toStringWithGetters: true)
class MyClass with _$MyClass {
  const factory MyClass({
    required int a,
    required int b,
  }) = _MyClass;

  const MyClass._();

  int get sum => a + b;
}

// or

@freezed
class MyClass with _$MyClass {
  const factory MyClass({
    required int a,
    required int b,
  }) = _MyClass;

  const MyClass._();

  @addToString
  int get sum => a + b;
}

Guida contributor