-1

Here is the api response which i want to show in my app but i'm unable to do so maybe some decoding issue but i could not fix this its throwing alot of errors !

[
  {
    "domains": [
        "marywood.edu"
    ],
    "alpha_two_code": "US",
    "country": "United States",
    "web_pages": [
        "http://www.marywood.edu"
    ],
    "name": "Marywood University",
    "state-province": null
},
{
    "domains": [
        "lindenwood.edu"
    ],
    "alpha_two_code": "US",
    "country": "United States",
    "web_pages": [
        "http://www.lindenwood.edu/"
    ],
    "name": "Lindenwood University",
    "state-province": null
  }
]

How to decode this json ?

Here is my code. I tried searching across the web but there are a lot of JSON formats so i was unable to do it .A little help would be appreciated

  Future<Api> fetchData() async {
  final response = await http.get(Uri.parse(
      'http://universities.hipolabs.com/search?country=United+States'));

  if (response.statusCode == 200) {
    return Api.fromJson(jsonDecode(response.body));
  } else {
    throw Exception('Failed to Load Data');
  }
}

class Api {
  final List<String> domains;
  final String code;
  final String country;
  final List<String> pages;
  final String name;
  final String? state;

  const Api({
    required this.domains,
    required this.code,
    required this.country,
    required this.pages,
    required this.name,
    required this.state,
  });

  factory Api.fromJson(Map<String, dynamic> json) {
    return Api(
      domains: json['domains'] as List<String>,
      code: json['alpha_two_code'] as String,
      country: json['country'] as String,
      pages: json['web_pages'] as List<String>,
      name: json['name'] as String,
      state: (json['state-province'] != null)
          ? json['state-province'] as String
          : "",
    );
  }
}

1 Answer 1

1

You can run over the response and parse the individual entries

Iterable it = json.decode(response.body);

List<Api> apis = List<Api>.from(it.map((model)=> Api.fromJson(model)));
Sign up to request clarification or add additional context in comments.

Comments

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.