Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unnecessary Non-null assertion operator in Builder #32

Open
Nico04 opened this issue Dec 2, 2024 · 0 comments
Open

Unnecessary Non-null assertion operator in Builder #32

Nico04 opened this issue Dec 2, 2024 · 0 comments

Comments

@Nico04
Copy link

Nico04 commented Dec 2, 2024

When using a default value instanciation, the build() method of the builder adds an unecessary "!", which may leads to a throw.
For instance with this class :

@JsonSerializable()
@DataClass(changeable: true, buildable: true)
class IbpMeasure extends WithArea with _$IbpMeasure {
 const IbpMeasure({
    required this.method,
    this.comments,
    String? version,
  }) : version = version ?? '3_0';
  factory IbpMeasure.fromJson(JsonObject json) => _$IbpMeasureFromJson(json);
  JsonObject toJson() => _$IbpMeasureToJson(this);

  final IbpMethod method;
  final String? comments;
  final String version;
}

The generated builder is :

IbpMeasure build() => IbpMeasure(
  method: method!,
  comments: comments,
  version: version!,
);

But for the version field, the "!" should be present at all, because in the constructor it's nullable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant