json_serializable.dart icon indicating copy to clipboard operation
json_serializable.dart copied to clipboard

Type casting ignores NNBD

Open hongyiweiwu opened this issue 6 years ago • 1 comments

Consider the code below

@JsonSerializable()
class Example {
    @JsonKey(nullable: true)
    List<String>? val;

    const Example(this.val);

    <boilerplate>
}

the generated fromJson method will look like

return Example((json['val'] as List)?.map(e => e as String)?.toList())

This currently results in a warning by dart analyzer since we're casting json['val'] to a non-null type List so the null-aware access operator isn't necessary. In this case though a more appropriate cast might be as List?.

hongyiweiwu avatar Jan 15 '20 23:01 hongyiweiwu

We're a way off from making this package ready for "null safety" (previously known as NNDB)

kevmoo avatar Jan 15 '20 23:01 kevmoo