I'm using Freezed to generate my models. I want to ignore a specific key, so I'm using @JsonKey(ignore: true).
@freezed
class ObjectA with _$ObjectA {
const factory ObjectA({
@JsonKey(ignore: true)
List<ObjectB> ObjectBList,
}) = _ObjectA;
factory ObjectA.fromJson(Map<String, dynamic> json) => _$ObjectA(json);
}
When I try to run the build_runner, I'm getting the above exception:
The parameter
ObjectBListofObjectA.is non-nullable but is neither required nor marked with @Default
Why doesn't it been ignored?
The issue you are facing is with
Dartsyntax. When you declare parameters in constructors. You can have anullableparameter or anon-nullableparameter; this also apply to variables.If you want the parameter you declared in the constructor to be
non-nullablethen it must either have a default value or the declared parameter must be prefixed with therequiredkeyword.You have three options:
requiredkeywordrequired List<ObjectB> ObjectBListList<ObjectB>? ObjectBListfreezedthis can be done with the@Defaultannotation@Default([]) List<ObjectB> ObjectBList