错误表明它无法创建ApplicableFlight
上的问题FlightSegmentReference
动态列表不是'Map String,dynamic类型的子类型
这是使用json.decode
和fromJson
的地方
var map = json.decode(response.body) as Map<String, dynamic>;
ResponseGate responseGate = new ResponseGate.fromJson(map);
这是json.decode
的输出片段
..,ApplicableFlight: {FlightReferences: F1,FlightSegmentReference: {ref: 0199,ClassOfService: {refs:ABCD,Code: {SeatsLeft: 6,content: Z},MarketingName:world}},OriginDestinationReferences:FF},
response.body
如下所示:
{ ..,
{
"ApplicableFlight": {
"FlightReferences": "F1",
"FlightSegmentReference": {
"ref": "0199",
"ClassOfService": {
"refs": "FBCODE1ADT",
"Code": {
"SeatsLeft": 7,
"content": "Z"
},
"MarketingName": "world"
}
},
"OriginDestinationReferences": "FF"
}
},}
这是定义对象并ResponseGate.fromJson(map)
寻找的地方
x.dart
@JsonSerializable()
class ResponseGate {
@JsonKey(name: "Offer")
List<Offer> offer;
ResponseGate(this.offer);
factory ResponseGate.fromJson(Map<String, dynamic> json) =>
_$ResponseGateFromJson(json);
}
@JsonSerializable()
class Offer {
@JsonKey(name: "PricedOffer")
PricedOffer pricedOffer;
Offer(this.pricedOffer);
factory Offer.fromJson(Map<String, dynamic> json) =>
_$OfferFromJson(json);
}
@JsonSerializable()
class PricedOffer {
@JsonKey(name: "Associations")
List<Associates> listOfAssociations;
PricedOffer(this.listOfAssociations);
factory PricedOffer.fromJson(Map<String, dynamic> json) =>
_$PricedOfferFromJson(json);
}
@JsonSerializable()
class Associates {
@JsonKey(name: "ApplicableFlight")
ApplicableFlight applicableFlight;
Associates(this.applicableFlight);
factory Associates.fromJson(Map<String, dynamic> json) =>
_$AssociatesFromJson(json);
}
@JsonSerializable()
class ApplicableFlight {
@JsonKey(name: "FlightReferences")
var flightReference;
@JsonKey(name: "FlightSegmentReference")
FlightSegmentReference flightSegmentReference;
@JsonKey(name: "OriginDestinationReferences")
var originDestinationReferences;
ApplicableFlight(this.flightReference, this.flightSegmentReference,
this.originDestinationReferences);
factory ApplicableFlight.fromJson(Map<String, dynamic> json) =>
_$ApplicableFlightFromJson(json);
}
@JsonSerializable()
class FlightSegmentReference {
@JsonKey(name: "ref")
var ref;
@JsonKey(name: "ClassOfService")
ClassOfService classOfService;
FlightSegmentReference(this.ref, this.classOfService);
factory FlightSegmentReference.fromJson(Map<String, dynamic> json) =>
_$FlightSegmentReferenceFromJson(json);
}
@JsonSerializable()
class ClassOfService {
@JsonKey(name: "refs")
var refs;
@JsonKey(name: "Code")
Code code;
@JsonKey(name: "MarketingName")
var marketingName;
ClassOfService(this.refs, this.code, this.marketingName);
factory ClassOfService.fromJson(Map<String, dynamic> json) =>
_$ClassOfServiceFromJson(json);
}
@JsonSerializable()
class Code {
@JsonKey(name: "SeatsLeft")
var seatLeft;
@JsonKey(name: "content")
var content;
Code(this.seatLeft, this.content);
factory Code.fromJson(Map<String, dynamic> json) => _$CodeFromJson(json);
}
x.g.dart
ApplicableFlight _$ApplicableFlightFromJson(Map<String, dynamic> 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 == null
? null
: FlightSegmentReference.fromJson(v as Map<String, dynamic>)),
$checkedConvert(json, 'OriginDestinationReferences', (v) => v));
return val;
}, fieldKeyMap: const {
'flightReference': 'FlightReferences',
'flightSegmentReference': 'FlightSegmentReference',
'originDestinationReferences': 'OriginDestinationReferences'
});
}
FlightSegmentReference _$FlightSegmentReferenceFromJson(
Map<String, dynamic> json) {
return $checkedNew('FlightSegmentReference', json, () {
$checkKeys(json, allowedKeys: const ['ref', 'ClassOfService']);
final val = FlightSegmentReference(
$checkedConvert(json, 'ref', (v) => v),
$checkedConvert(
json,
'ClassOfService',
(v) => v == null
? null
: ClassOfService.fromJson(v as Map<String, dynamic>)));
return val;
}, fieldKeyMap: const {'classOfService': 'ClassOfService'});
}
ClassOfService _$ClassOfServiceFromJson(Map<String, dynamic> json) {
return $checkedNew('ClassOfService', json, () {
$checkKeys(json, allowedKeys: const ['refs', 'Code', 'MarketingName']);
final val = ClassOfService(
$checkedConvert(json, 'refs', (v) => v),
$checkedConvert(json, 'Code',
(v) => v == null ? null : Code.fromJson(v as Map<String, dynamic>)),
$checkedConvert(json, 'MarketingName', (v) => v));
return val;
}, fieldKeyMap: const {'code': 'Code', 'marketingName': 'MarketingName'});
}
Code _$CodeFromJson(Map<String, dynamic> json) {
return $checkedNew('Code', json, () {
$checkKeys(json, allowedKeys: const ['SeatsLeft', 'content']);
final val = Code($checkedConvert(json, 'SeatsLeft', (v) => v),
$checkedConvert(json, 'content', (v) => v));
return val;
}, fieldKeyMap: const {'seatLeft': 'SeatsLeft'});
}
已更新
我看了看api的响应,发现FlightSegmentReference
如下所示以两种不同的方式出现。我该如何设计这样的格式?
{
"Offer": [
{
"PricedOffer": {
"Associations": [
{
"ApplicableFlight": {
"FlightReferences": "Flight1",
"FlightSegmentReference": {
"ref": "99",
"ClassOfService": {
"refs": "ADT",
"Code": {
"SeatsLeft": 9,
"content": "J"
},
"MarketingName": " World"
}
},
"OriginDestinationReferences": "OD1"
}
},
{
"ApplicableFlight": {
"FlightReferences": "Flight11",
"FlightSegmentReference": {
"ref": "BA0138",
"ClassOfService": {
"refs": "FBCODE1ADT",
"Code": {
"SeatsLeft": 9,
"content": "J"
},
"MarketingName": "Club World"
}
},
"OriginDestinationReferences": "OD2"
}
}
]
}
},
{
"PricedOffer": {
"Associations": [
{
"ApplicableFlight": {
"FlightReferences": "Fl",
"FlightSegmentReference": [
{
"ref": "02",
"ClassOfService": {
"refs": "FBCODE2ADT",
"Code": {
"SeatsLeft": 9,
"content": "J"
},
"MarketingName": "Club World"
}
},
{
"ref": "58",
"ClassOfService": {
"refs": "1ADT",
"Code": {
"SeatsLeft": 9,
"content": "J"
},
"MarketingName": "B"
}
}
],
"OriginDestinationReferences": "1"
}
},
{
"ApplicableFlight": {
"FlightReferences": "Fl11",
"FlightSegmentReference": {
"ref": "8",
"ClassOfService": {
"refs": "E1ADT",
"Code": {
"SeatsLeft": 9,
"content": "J"
},
"MarketingName": "orld"
}
},
"OriginDestinationReferences": "O"
}
}
]
}
}
]}
答案 0 :(得分:0)
List<dynamic> areas = new List();
areas = json.decode(response.body);
String _mySelection1;
Container(
height: 50,
child: DropdownButton<String>(
isExpanded: true,
isDense: true,
hint: new Text("Select Location"),
value: _mySelection1,
onChanged:(String newValue) {
setState(() {
_mySelection1 = newValue;
});
print (_mySelection1);
},
items: areas.map((map){
return DropdownMenuItem<String>(
value: map["area_id"],
child: Text(
map["name"],
),
);
}).toList(),
),
),
这是解决此类问题的示例。.我的json也收到Mam响应。但是我是用这种方式来处理的。希望这个例子对你有帮助。这是我的答复。
[{"id":"1","name":"Dermatology"},{"id":"2","name":"Cancer of Female Reproductive System"},{"id":"3","name":"Cardiology"},{"id":"4","name":"Dentistry"},{"id":"5","name":"Allergy & Immunology"},{"id":"6","name":"Anesthesia"},{"id":"7","name":"Colorectal Surgery"},{"id":"8","name":"Endocrinology (Diabetes, Hormones, Thyroid, etc.)"},{"id":"9","name":"ENT (Ear, Nose & Throat, Otorhinolaryngology)"},{"id":"10","name":"Gastroenterology (Stomach, Pancreas and Intestine)"},{"id":"11","name":"General Physician"}]
答案 1 :(得分:0)
它在错误消息Map for List中可能需要列表
比它匹配的要大,因此可以是子类型