google / json_serializable.dart

Generates utilities to aid in serializing to/from JSON.
https://pub.dev/packages/json_serializable
BSD 3-Clause "New" or "Revised" License
1.55k stars 396 forks source link

Generated argument for nullable argument in fromJson is not nullable #884

Closed nzackoya closed 3 years ago

nzackoya commented 3 years ago

For example we have fromJson for a field:

@JsonKey(fromJson: _latLngFromJson)
  final LatLng? location;

so generating from this

_latLngFromJson(Map<String, dynamic>? geoPoint)

we get non nullable

..location =
        UserRecord._latLngFromJson(json['location'] as Map<String, dynamic>)

Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 2.0.5, on macOS 11.2.3 20D91 darwin-x64, locale en-GB) [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0) [✓] Xcode - develop for iOS and macOS [✓] Chrome - develop for the web [✓] Android Studio (version 4.1) [✓] Connected device (2 available)

flutter pub deps -- --style=compact Dart SDK 2.12.3 Flutter SDK 2.0.5

dependencies:

dev dependencies:

transitive dependencies:

DrNorton commented 3 years ago

I have same problem. @JsonKey( fromJson: getShapeDtoFromJson, toJson: getJsonFromShapeDto) final NgApiShapeDto? geometry;

It generate geometry: getShapeDtoFromJson(json['geometry'] as Map<String, dynamic>)

nzackoya commented 3 years ago

I have same problem. @JsonKey( fromJson: getShapeDtoFromJson, toJson: getJsonFromShapeDto) final NgApiShapeDto? geometry;

It generate geometry: getShapeDtoFromJson(json['geometry'] as Map<String, dynamic>)

Hey, as a workaround just change Map<String, dynamic> to simply Map in your getShapeDtoFromJson. It will generate as Map?

DrNorton commented 3 years ago

I changed to NgApiShapeDto? getShapeDtoFromJson(Map? json)

It generated - getShapeDtoFromJson(json['geometry'] as Map?),

Thank you. Its great

kevmoo commented 3 years ago

This has been fixed in 4.1.3