Praveen Kumar
Praveen Kumar

Reputation: 299

How to fetch complex json data by specific names in flutter

https://api.covid19api.com/summary This is the API I am using now I can fetch the global data by the below code I want to fetch data of a single Country(India) by the same method. If there is no method by which I can get the data then if I use "https://api.covid19api.com/total/dayone/country/India" then how to get the daily confirmed cases.?

class GlobalSummaryModel{
  final int newConfirmed;
  final int totalConfirmed;
  final int newDeaths;
  final int totalDeaths;
  final int newRecovered;
  final int totalRecovered;
  final DateTime date;

  GlobalSummaryModel(this.newConfirmed, this.totalConfirmed, this.newDeaths, this.totalDeaths, this.newRecovered, this.totalRecovered, this.date);

  factory GlobalSummaryModel.fromJson(Map<String, dynamic> json){
    return GlobalSummaryModel(
      json["Global"]["NewConfirmed"],
      json["Global"]["TotalConfirmed"],
      json["Global"]["NewDeaths"],
      json["Global"]["TotalDeaths"],
      json["Global"]["NewRecovered"],
      json["Global"]["TotalRecovered"],
      DateTime.parse(json["Date"]),
    );
  }
}

Please provide me the code if you can that will be more helpful for me I am new in fetching data from the rest API.

Upvotes: 2

Views: 251

Answers (1)

Michael Horn
Michael Horn

Reputation: 4099

The API also returns a Countries field in the response, which contains data for India. You can extract that data like so:

final countries = json["Countries"];
final Map<String, dynamic> indiaSummaryData = countries.firstWhere((map) {
  return map["CountryCode"] == "IN";
});

Upvotes: 0

Related Questions