-
Notifications
You must be signed in to change notification settings - Fork 3
/
Api.dart
113 lines (103 loc) · 3.11 KB
/
Api.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class API {
final int updated;
final int cases;
final int todayCases;
final int deaths;
final int todayDeath;
final int todayRecovered;
final int active;
API(
{this.active,
this.cases,
this.deaths,
this.todayCases,
this.todayDeath,
this.todayRecovered,
this.updated});
factory API.fromJson(Map<String, dynamic> json) {
return API(
updated: json['updated'],
cases: json['cases'],
todayCases: json['todayCases'],
deaths: json['deaths'],
todayDeath: json['todayDeaths'],
todayRecovered: json['todayRecovered'],
active: json['active'],
);
}
}
Future<API> fetchApi() async {
final response = await http.get('https://disease.sh/v2/all');
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
return API.fromJson(json.decode(response.body));
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to load album');
}
}
class HttpfetchAPI extends StatefulWidget {
@override
_HttpfetchAPIState createState() => _HttpfetchAPIState();
}
class _HttpfetchAPIState extends State<HttpfetchAPI> {
Future<API> futureAPI;
@override
void initState() {
super.initState();
futureAPI = fetchApi();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: FutureBuilder<API>(
future: futureAPI,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Container(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Active cases'),
SizedBox(width: 10.0),
Text(snapshot.data.active.toString())
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Total cases'),
SizedBox(width: 10.0),
Text(snapshot.data.todayCases.toString())
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Total Deaths'),
SizedBox(width: 10.0),
Text(snapshot.data.todayDeath.toString())
],
),
],
),
),
);
} else if (snapshot.hasError) return Text("${snapshot.error}");
return CircularProgressIndicator();
},
),
),
);
}
}