freezed icon indicating copy to clipboard operation
freezed copied to clipboard

Make Fields non nullale provided with Default value.

Open f1forhelp opened this issue 11 months ago • 2 comments

In the following code, I'd like the parameters to be nullable. This means that when I pass null or nothing to the parameters, default values should be used.

@freezed
class User with _$User {
  factory User({
    @Default(0) int? id,
    @Default("") String? userName,
    @Default("") String? image,
    @Default("") String? token,
  }) = _User;
}

Currently, when I implement this, it functions correctly. However, since default values are already provided, the generated code should not include nullable fields.

For example:

User user = User();
user.id // This field should not be nullable, as default values have already been provided.

Basically I don't want to use non nullable operator in UI.

user.id!

f1forhelp avatar Mar 12 '24 22:03 f1forhelp

@rrousselGit please help with suggestion

f1forhelp avatar Mar 13 '24 06:03 f1forhelp

No, it should be nullable if we pass null to it.

For example, the maxLine property is default to 1, however, we can pass null and its behavior changed.

Cierra-Runis avatar Mar 29 '24 23:03 Cierra-Runis