1
0
mirror of https://github.com/nisrulz/flutter-examples.git synced 2025-11-09 04:58:58 +00:00

Added covid-19 example app and Updated the readme (#73)

This commit is contained in:
Mohammed Mehdi
2021-07-26 00:26:58 +05:30
committed by GitHub
parent 48f14caf15
commit 3eaedb619c
72 changed files with 2166 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
import 'dart:convert';
import 'package:http/http.dart' as http;
// function to fetch global Corona Virus(all countries together)
// to fetch data of a particular country(in this case, India)
Future getAllData() async {
try {
var allCountriesUrl = 'https://corona.lmao.ninja/v2/all';
var allCountriesResponse = await http.get(allCountriesUrl);
var particularCountryUrl = 'https://corona.lmao.ninja/v2/historical/India';
var particularCountryResponse = await http.get(particularCountryUrl);
// a list to store the data points
List<Map<String, dynamic>> dataPoints = [];
jsonDecode(particularCountryResponse.body)['timeline']['cases']
.forEach((k, v) {
List a = k.split('/');
DateTime b = DateTime(int.parse(a[2]), int.parse(a[0]), int.parse(a[1]));
dataPoints.add({"date": b, "value": v});
});
List<Map<String, dynamic>> dataPoints1 = [];
jsonDecode(particularCountryResponse.body)['timeline']['deaths']
.forEach((k, v) {
List a = k.split('/');
DateTime b = DateTime(int.parse(a[2]), int.parse(a[0]), int.parse(a[1]));
dataPoints1.add({"date": b, "value": v});
});
List<Map<String, dynamic>> dataPoints2 = [];
jsonDecode(particularCountryResponse.body)['timeline']['recovered']
.forEach((k, v) {
List a = k.split('/');
DateTime b = DateTime(int.parse(a[2]), int.parse(a[0]), int.parse(a[1]));
dataPoints2.add({"date": b, "value": v});
});
return {
"all": allCountriesResponse.body,
"country": dataPoints,
"deaths": dataPoints1,
"recovered": dataPoints2
};
} catch (e) {
print(e);
}
}
// function to fetch data of all countries(country wise)
Future getAllCountriesData() async {
try {
var url = 'https://corona.lmao.ninja/v2/countries?sort=country';
var response = await http.get(url);
return response.body;
} catch (e) {
print(e);
}
}