Open
Description
Using json_serializable: ^6.7.1
on a model class which extends Equatable
leads to unwanted fields being included in generated code.
Given:
@JsonSerializable(createFactory: false) // <- notice createFactory being false
@immutable
class User extends Equatable {
const User({
required this.id,
required this.name,
required this.username,
required this.password,
});
final String id;
final String name;
final String username;
final String password;
@override
List<Object?> get props => [id, name, username, password];
}
the output is
Map<String, dynamic> _$UserToJson(User instance) {
final val = <String, dynamic>{};
void writeNotNull(String key, dynamic value) {
if (value != null) {
val[key] = value;
}
}
writeNotNull('stringify', instance.stringify); // <- should not be included
val['hashCode'] = instance.hashCode; // <- should not be included
val['id'] = instance.id;
val['name'] = instance.name;
val['username'] = instance.username;
val['password'] = instance.password;
val['props'] = instance.props; // <- should not be included
return val;
}
with multiple unwanted fields being included in the json output.
Hint: When using @JsonSerializable()
or @JsonSerializable(createToJson: false)
all works as expected.