x.g.dart
是基于波纹管类ApplicableFlight
生成的。我要实现的是,如果json文件中的FlightSegmentReference
是map而不是list,我希望它创建一个列表并将该map添加到list中并返回它。
目前,它仅以这种方式处理FlightSegmentReference
的列表。我想保持ApplicableFlight
类不变,但是如果地图出现,我想创建一个列表并添加地图。
x.g.dart
ApplicableFlight _$ApplicableFlightFromJson(Map json) {
return $checkedNew('ApplicableFlight', json, () {
$checkKeys(json, allowedKeys: const [
'FlightReferences',
'FlightSegmentReference',
'OriginDestinationReferences'
]);
final val = ApplicableFlight(
$checkedConvert(json, 'FlightReferences', (v) => v),
$checkedConvert(
json,
'FlightSegmentReference',
(v) => (v as List)
?.map((e) => e == null
? null
: FlightSegmentReference.fromJson((e as Map)?.map(
(k, e) => MapEntry(k as String, e),
)))
?.toList()),
$checkedConvert(json, 'OriginDestinationReferences', (v) => v),
);
return val;
}, fieldKeyMap: const {
'flightReference': 'FlightReferences',
'flightSegmentReference': 'FlightSegmentReference',
'originDestinationReferences': 'OriginDestinationReferences'
});
}
这是
@JsonSerializable()
class ApplicableFlight {
@JsonKey(name: "FlightReferences")
var flightReference;
@JsonKey(name: "FlightSegmentReference")
List<FlightSegmentReference> flightSegmentReference;
@JsonKey(name: "OriginDestinationReferences")
var originDestinationReferences;
ApplicableFlight(this.flightReference, this.flightSegmentReference,
this.originDestinationReferences);
factory ApplicableFlight.fromJson(Map<String, dynamic> json) =>
_$ApplicableFlightFromJson(json);
}