Reputation: 602
I am trying to get data from an API here. The sample data in the endpoint looks like:
`{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
]
}
}`
I am trying to map this data to the CovidData
class in the following code:
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"
});
}
}
When I navigate to this page I get the following exception:
[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic>
I am new with APIs so I don't know how to solve this problem. Any help would be appreciate. Thanks.
Upvotes: 0
Views: 503
Reputation: 1896
json.decode(response.body)
returns you a Map type object which is internally represented in dart as _InternalLinkedHashMap<String, dynamic>
. You can see the structure of this map from the link to the API you provided. The data you want is inside this Map type object under the field data
then 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])
}
);
The error you are getting is due to calling the method signature of .map
that belongs to the Iterable
class (https://api.flutter.dev/flutter/dart-core/Iterable/map.html) on a Map type object, which is not an Iterable.
Upvotes: 1