rrousselGit/freezed

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

Open

#698 opened on Jul 6, 2022

 (1 comment) (0 reactions) (1 assignee)Dart (300 forks)github user discovery
enhancementgood first issue

Repository metrics

Stars
 (2,177 stars)
PR merge metrics
 (PR metrics pending)

Description

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;
}

Contributor guide