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

FEATURE: Only optional fields nullable in generate message constructor #819

Open
duck-dev-go opened this issue Apr 17, 2023 · 1 comment

Comments

@duck-dev-go
Copy link

Description:

Currently, in the Dart protobuf implementation, all fields are nullable in the constructor of message classes, even if they are not optional. This makes it hard to know before runtime if a required field has been assigned, which can lead to runtime errors and harder-to-debug code.

I propose adding a feature that enforces nullability checks in the constructor of message classes based on whether the fields are optional or required. This would make it easier for developers to ensure that all required fields have been assigned before runtime.

Example:

Consider the following .proto file:

syntax = "proto3";

message Person {
  string name = 1;
  int32 age = 2;
  optional string address = 3;
}

The generated Dart code should have a constructor that enforces null checks for required fields, like so:

class Person extends $pb.GeneratedMessage {
  Person({
    required String name,
    required int age,
    String? address,
  }) : super() {
    this.name = name;
    this.age = age;
    if (address != null) {
      this.address = address;
    }
  }

  // ... rest of the generated code ...
}

This would allow developers to ensure that required fields have been assigned before creating an instance of the message class, preventing runtime errors and making the code more robust.

@duck-dev-go duck-dev-go changed the title Enforce nullability for optional fields in message constructors FEATURE: Enforce nullability for optional fields in message constructors Apr 17, 2023
@duck-dev-go duck-dev-go changed the title FEATURE: Enforce nullability for optional fields in message constructors FEATURE: Only optional fields nullable in generate message constructor Apr 17, 2023
@oo-work
Copy link

oo-work commented Nov 30, 2024

Same issue

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

2 participants