0

Am looking to use a public [api] for a personal project. This is a snippet of a full json data set.

"Countries": [
    {
      "Country": "ALA Aland Islands",
      "CountryCode": "AX",
      "Slug": "ala-aland-islands",
      "NewConfirmed": 0,
      "TotalConfirmed": 0,
      "NewDeaths": 0,
      "TotalDeaths": 0,
      "NewRecovered": 0,
      "TotalRecovered": 0,
      "Date": "2020-04-05T06:37:00Z"
    },

I've tried to access the object in the array, I even tried some answers I saw here, but it's not working for me either I keep getting my "loading data..." screen or I get a white screen with no errors. My code snippet below

class CountriesData {
  String country;
  String countryCode;
  int confirmed;
  int deaths;
  int recovered;
  int active;
  DateTime date;

  CountriesData(this.country, this.countryCode, this.confirmed, this.deaths, 
      this.recovered, this.active, this.date);

  CountriesData.json(Map<String, dynamic> json) {
    country = json['Country'];
    countryCode = json['CountryCode'];
    confirmed = json['Confirmed'];
    deaths = json['Deaths'];
    recovered = json['Recovered'];
    active = json['Active'];
    date = DateTime.parse(json['Date']);
  }
}

1 Answer 1

1

It seems that you never parsed the JSON. To use the values from JSON, you need to first convert to Map. You have to import dart:convert library and then you can use the function json.decode(jsonData). This returns a map that you can use to fetch data from.

var jsonData = fetchJson();
var parsedJson = json.decode(jsonData);
var x = CountriesData.json(parsedJson);
Sign up to request clarification or add additional context in comments.

2 Comments

am I supposed to create the method for the fetchJson(), cos it's throwing a create method error
No, that's just an example. Sorry if I wasn't clear

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.