rrousselGit/freezed

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

开放

#698 创建于 2022年7月6日

 (1 条评论) (0 个反应) (1 位负责人)Dart (300 个派生)github user discovery
enhancementgood first issue

仓库指标

星标
 (2,177 个星标)
PR 合并指标
 (平均合并 25天 18小时) (30 天内合并 1 个 PR)

描述

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

贡献者指南