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
: "",
);
}
}