Flutter 将地图列表转换为dart中的对象列表
我正在向API发出请求,该API将返回一个响应Flutter 将地图列表转换为dart中的对象列表,flutter,dart,Flutter,Dart,我正在向API发出请求,该API将返回一个响应 final response = await http.get(Uri.parse(requestUrl), headers: headers); { "meta": { "upcomingMatchCount": 5, "inProgressMatchCount": 10, "completedMatchCount": 5 }, &qu
final response = await http.get(Uri.parse(requestUrl), headers: headers);
{
"meta": {
"upcomingMatchCount": 5,
"inProgressMatchCount": 10,
"completedMatchCount": 5
},
"matchList": {
"matches": [
{
"id": 49944,
"matchTypeId": 15,
"series": {
"id": 2739,
"name": "LV= Insurance County Championship 2021",
"shortName": "LV= Insurance County Championship 2021"
},
"name": "",
"status": "LIVE",
"venue": {
"name": "The Cooper Associates County Ground",
"shortName": "The Cooper Associates County Ground"
},
"homeTeam": {
"isBatting": true,
"id": 55,
"name": "Somerset",
"shortName": "SOM"
},
"awayTeam": {
"isBatting": false,
"id": 46,
"name": "Gloucestershire",
"shortName": "GLO"
},
"currentMatchState": "Live",
"isMultiDay": true,
"matchSummaryText": "Live: Gloucestershire won the toss and elected to bowl.",
"scores": {
"homeScore": "8-293",
"homeOvers": "88.0",
"awayScore": "0-0",
"awayOvers": "0"
},
"liveStreams": [],
"isLive": false,
"currentInningId": 1,
"isMatchDrawn": false,
"isMatchAbandoned": false,
"startDateTime": "2021-04-15T10:00:00Z",
"endDateTime": "2021-04-18T17:00:00Z",
"isWomensMatch": false,
"isGamedayEnabled": false,
"removeMatch": false
},
{
"id": 49942,
"matchTypeId": 15,
"series": {
"id": 2739,
"name": "LV= Insurance County Championship 2021",
"shortName": "LV= Insurance County Championship 2021"
},
"name": "",
"status": "LIVE",
"venue": {
"name": "The Spitfire Ground, St Lawrence",
"shortName": "The Spitfire Ground, St Lawrence"
},
"homeTeam": {
"isBatting": false,
"id": 45,
"name": "Kent",
"shortName": "KEN"
},
"awayTeam": {
"isBatting": true,
"id": 40,
"name": "Yorkshire",
"shortName": "YRK"
},
"currentMatchState": "Live",
"isMultiDay": true,
"matchSummaryText": "Live: Yorkshire won the toss and elected to bat.",
"scores": {
"homeScore": "0-0",
"homeOvers": "0",
"awayScore": "8-358",
"awayOvers": "100.0"
},
"liveStreams": [],
"isLive": false,
"currentInningId": 1,
"isMatchDrawn": false,
"isMatchAbandoned": false,
"startDateTime": "2021-04-15T10:00:00Z",
"endDateTime": "2021-04-18T17:00:00Z",
"isWomensMatch": false,
"isGamedayEnabled": false,
"removeMatch": false
},
]
它返回以下响应
final response = await http.get(Uri.parse(requestUrl), headers: headers);
{
"meta": {
"upcomingMatchCount": 5,
"inProgressMatchCount": 10,
"completedMatchCount": 5
},
"matchList": {
"matches": [
{
"id": 49944,
"matchTypeId": 15,
"series": {
"id": 2739,
"name": "LV= Insurance County Championship 2021",
"shortName": "LV= Insurance County Championship 2021"
},
"name": "",
"status": "LIVE",
"venue": {
"name": "The Cooper Associates County Ground",
"shortName": "The Cooper Associates County Ground"
},
"homeTeam": {
"isBatting": true,
"id": 55,
"name": "Somerset",
"shortName": "SOM"
},
"awayTeam": {
"isBatting": false,
"id": 46,
"name": "Gloucestershire",
"shortName": "GLO"
},
"currentMatchState": "Live",
"isMultiDay": true,
"matchSummaryText": "Live: Gloucestershire won the toss and elected to bowl.",
"scores": {
"homeScore": "8-293",
"homeOvers": "88.0",
"awayScore": "0-0",
"awayOvers": "0"
},
"liveStreams": [],
"isLive": false,
"currentInningId": 1,
"isMatchDrawn": false,
"isMatchAbandoned": false,
"startDateTime": "2021-04-15T10:00:00Z",
"endDateTime": "2021-04-18T17:00:00Z",
"isWomensMatch": false,
"isGamedayEnabled": false,
"removeMatch": false
},
{
"id": 49942,
"matchTypeId": 15,
"series": {
"id": 2739,
"name": "LV= Insurance County Championship 2021",
"shortName": "LV= Insurance County Championship 2021"
},
"name": "",
"status": "LIVE",
"venue": {
"name": "The Spitfire Ground, St Lawrence",
"shortName": "The Spitfire Ground, St Lawrence"
},
"homeTeam": {
"isBatting": false,
"id": 45,
"name": "Kent",
"shortName": "KEN"
},
"awayTeam": {
"isBatting": true,
"id": 40,
"name": "Yorkshire",
"shortName": "YRK"
},
"currentMatchState": "Live",
"isMultiDay": true,
"matchSummaryText": "Live: Yorkshire won the toss and elected to bat.",
"scores": {
"homeScore": "0-0",
"homeOvers": "0",
"awayScore": "8-358",
"awayOvers": "100.0"
},
"liveStreams": [],
"isLive": false,
"currentInningId": 1,
"isMatchDrawn": false,
"isMatchAbandoned": false,
"startDateTime": "2021-04-15T10:00:00Z",
"endDateTime": "2021-04-18T17:00:00Z",
"isWomensMatch": false,
"isGamedayEnabled": false,
"removeMatch": false
},
]
我正在从该响应中检索匹配列表,如下所示:
final list = map['matchList']['matches'] as List;
我有一个模型类,它表示matches键中的每个匹配项:
class MatchModel {
int id;
int matchTypeId;
Series series;
String name;
String status;
Venue venue;
HomeTeam homeTeam;
HomeTeam awayTeam;
String currentMatchState;
bool isMultiDay;
String matchSummaryText;
Scores scores;
List<Null> liveStreams;
bool isLive;
int currentInningId;
bool isMatchDrawn;
bool isMatchAbandoned;
String startDateTime;
String endDateTime;
bool isWomensMatch;
bool isGamedayEnabled;
bool removeMatch;
MatchModel(
{this.id,
this.matchTypeId,
this.series,
this.name,
this.status,
this.venue,
this.homeTeam,
this.awayTeam,
this.currentMatchState,
this.isMultiDay,
this.matchSummaryText,
this.scores,
this.liveStreams,
this.isLive,
this.currentInningId,
this.isMatchDrawn,
this.isMatchAbandoned,
this.startDateTime,
this.endDateTime,
this.isWomensMatch,
this.isGamedayEnabled,
this.removeMatch});
MatchModel.fromJson(Map<String, dynamic> json) {
id = json['id'];
matchTypeId = json['matchTypeId'];
series =
json['series'] != null ? new Series.fromJson(json['series']) : null;
name = json['name'];
status = json['status'];
venue = json['venue'] != null ? new Venue.fromJson(json['venue']) : null;
homeTeam = json['homeTeam'] != null
? new HomeTeam.fromJson(json['homeTeam'])
: null;
awayTeam = json['awayTeam'] != null
? new HomeTeam.fromJson(json['awayTeam'])
: null;
currentMatchState = json['currentMatchState'];
isMultiDay = json['isMultiDay'];
matchSummaryText = json['matchSummaryText'];
scores =
json['scores'] != null ? new Scores.fromJson(json['scores']) : null;
if (json['liveStreams'] != null) {
liveStreams = new List<Null>();
json['liveStreams'].forEach((v) {
});
}
isLive = json['isLive'];
currentInningId = json['currentInningId'];
isMatchDrawn = json['isMatchDrawn'];
isMatchAbandoned = json['isMatchAbandoned'];
startDateTime = json['startDateTime'];
endDateTime = json['endDateTime'];
isWomensMatch = json['isWomensMatch'];
isGamedayEnabled = json['isGamedayEnabled'];
removeMatch = json['removeMatch'];
}
类匹配模型{
int-id;
int matchTypeId;
系列;
字符串名;
字符串状态;
场地;;
主队主队;
主队awayTeam;
字符串当前匹配状态;
布尔是最后一天;
字符串匹配摘要文本;
得分;
列出liveStreams;
布尔伊斯利夫;
int currentiningid;
布尔·伊斯马特;
布尔·伊斯马奇;
字符串startDateTime;
字符串endDateTime;
布尔是女子比赛;
bool-isgamedaydenabled;
布尔删除匹配;
匹配模型(
{this.id,
这个.matchTypeId,
这个系列,,
这个名字,
这个,身份,,
这个场地,
这个,主队,
这是阿瓦泰姆,
这个州,
今天是最后一天,
此.matchSummaryText,
这是我的分数,
这是liveStreams,
这是我的生活,
这是我的名字,
这是我画的,
这是我放弃的,
这是开始时间,
这个.endDateTime,
这是一场女子比赛,
此.isgameday已启用,
此参数为.removeMatch});
MatchModel.fromJson(映射json){
id=json['id'];
matchTypeId=json['matchTypeId'];
系列=
json['series']!=null?新系列。fromJson(json['series']):null;
name=json['name'];
status=json['status'];
地点=json['venture']!=null?新地点。fromJson(json['venture']):null;
homeTeam=json['homeTeam']!=null
?新的HomeTeam.fromJson(json['HomeTeam'])
:null;
awayTeam=json['awayTeam']!=null
?新的HomeTeam.fromJson(json['awayTeam'])
:null;
currentMatchState=json['currentMatchState'];
isMultiDay=json['isMultiDay'];
matchSummaryText=json['matchSummaryText'];
得分=
json['scores']!=null?新分数。fromJson(json['scores']):null;
if(json['liveStreams']!=null){
liveStreams=新列表();
json['liveStreams'].forEach((v){
});
}
isLive=json['isLive'];
currentInningId=json['currentInningId'];
IsMatchDrawed=json['IsMatchDrawed'];
IsMatch放弃=json['IsMatch放弃'];
startDateTime=json['startDateTime'];
endDateTime=json['endDateTime'];
isWomensMatch=json['isWomensMatch'];
isGamedayEnabled=json['isGamedayEnabled'];
removeMatch=json['removeMatch'];
}
如何将数据从匹配列表映射到匹配模型列表?如果您还需要其他帮助,请务必告诉我。我建议使用JSON序列化生成代码。这样,您只需为类、构造函数和
部分“match.g.dart”;
在此之后,json_serializable
包将为您生成json转换器方法/工厂
有关更多信息,您可以使用本文:。问题是返回的响应对象实际上是一个字符串,因此您需要首先使用like将其转换为json
var json = jsonDecode(response).
Once you have it in json format what you can do is access the list as json['matchList']['matches']. So now you can iterater over it like
List<MatchModel> matches = []
for(var match in json['matchList']['matches']){
matches.add(MatchModel.fromJson(match));
}
Hope it's useful.
var json=jsonDecode(响应)。
一旦你有了json格式,你能做的就是以json['matchList']['matches']的形式访问列表
列出匹配项=[]
for(json['matchList']['matches']中的变量匹配){
add(MatchModel.fromJson(match));
}
希望有用。
试试这个
var json = jsonDecode(response.body)['matchList']['matches'];
List<MatchModel> matches = List.from(json).map((e) => MatchModel.fromJson(Map.from(e))).toList();
var json=jsonDecode(response.body)['matchList']['matches'];
List matches=List.from(json).map((e)=>MatchModel.fromJson(map.from(e))).toList();