我正在尝试从API here获取数据。端点中的样本数据如下:
`{4 items
"error":false
"statusCode":200
"message":"OK"
"data":{2 items
"lastChecked":"2020-04-02T11:49:38.233Z"
"covid19Stats":[15 items
0:{8 items
"city":""
"province":"Alberta"
"country":"Canada"
"lastUpdate":"2020-04-01 22:04:44"
"keyId":"Alberta, Canada"
"confirmed":754
"deaths":9
"recovered":0
}
1:{...}8 items
2:{...}8 items
3:{...}8 items
4:{...}8 items
5:{...}8 items
6:{...}8 items
7:{...}8 items
8:{...}8 items
9:{...}8 items
10:{...}8 items
11:{...}8 items
12:{...}8 items
13:{...}8 items
14:{...}8 items
]
}
}`
我正在尝试通过以下代码将此数据映射到CovidData
类:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class StatusPage extends StatefulWidget{
@override
State<StatefulWidget> createState() {
return StatusPageState();
}
}
class StatusPageState extends State<StatusPage>{
@override
void initState() {
super.initState();
fetch();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Center(
child: Text(
"COVID-19 Stats",
style: TextStyle(
color: Colors.white,
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold),
),
),
),
body: Container(
child: FlatButton(onPressed: () => show(), child: Text("Fetch")),
),
);
}
fetch(){
API.getStats().then(
(response) => {
lists = json.decode(response.body),
countries = lists.map((model) => CovidData.fromJson(model)).toList(),
// print(json.decode(response.body)['data']['covid19Stats'][0])
}
);
setState(() {
});
}
}
class CovidData{
String city = "";
String province = "";
String country = "";
String lastUpdate = "";
String keyId = "";
int confirmed = 0;
int deaths = 0;
int recovered = 0;
CovidData(String city, String province, String country, String lastUpdate, String keyId, int confirmed, int deaths, int recovered){
this.city = city;
this.province = province;
this.country = country;
this.lastUpdate = lastUpdate;
this.keyId = keyId;
this.confirmed = confirmed;
this.deaths = deaths;
this.recovered = recovered;
}
CovidData.fromJson(Map json)
: city = json['city'],
province = json['province'],
country = json['country'],
lastUpdate = json['lastUpdate'],
keyId = json['keyId'],
confirmed = json['confirmed'],
deaths = json['deaths'],
recovered = json['recovered'];
Iterable toJson(){
return [
{'city': city},
{'province': province},
{'country': country},
{'lastUpdate': lastUpdate},
{'keyId': keyId},
{'confirmed': confirmed},
{'deaths': deaths},
{'recovered': recovered},
];
}
}
List<CovidData> countries = [];
Iterable lists;
show(){
print(countries);
}
const baseUrl = "https://covid-19-coronavirus-statistics.p.rapidapi.com/v1/stats";
class API {
static Future getStats(){
return http.get(baseUrl, headers: {
"x-rapidapi-host": "covid-19-coronavirus-statistics.p.rapidapi.com",
"x-rapidapi-key": "8ca140a965mshe408a2e58737ba5p14b104jsn19a57561ec85"
});
}
}
导航到此页面时,出现以下异常:
[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic>
我是API的新手,所以我不知道如何解决此问题。任何帮助将不胜感激。谢谢。
答案 0 :(得分:1)
json.decode(response.body)
返回一个Map类型的对象,该对象在dart中内部表示为_InternalLinkedHashMap<String, dynamic>
。您可以通过指向您提供的API的链接查看此地图的结构。您想要的数据在此Map类型对象内的字段data
下,然后在covid19Stats
下。
API.getStats().then(
(response) => {
var response = json.decode(response.body),
countries = response['data']['covid19Stats'].map((model) => CovidData.fromJson(model)).toList(),
// print(json.decode(response.body)['data']['covid19Stats'][0])
}
);
您遇到的错误是由于在Map类型对象上调用了属于.map
类(https://api.flutter.dev/flutter/dart-core/Iterable/map.html)的Iterable
的方法签名,该方法签名不是Iterable。