Skip to content

Instantly share code, notes, and snippets.

@JoeCodeswell
Last active October 7, 2020 20:02
Show Gist options
  • Save JoeCodeswell/2f492c65c15a299f3176b06b0d178d5c to your computer and use it in GitHub Desktop.
Save JoeCodeswell/2f492c65c15a299f3176b06b0d178d5c to your computer and use it in GitHub Desktop.
C19data.dart
/// C19data.dart
/// [gist](https://gist.github.com/JoeCodeswell/2f492c65c15a299f3176b06b0d178d5c)
/// [Dartpad](https://dartpad.dev/2f492c65c15a299f3176b06b0d178d5c)
/// Predecessor TimeSeriesC19.dart
/// C19app1Pj\jchart_c19\lib\c19_data.dart
/// [gist](https://gist.github.com/JoeCodeswell/cdff0e68ae266fa624eaac5c286f20b2)
/// https://dartpad.dev/cdff0e68ae266fa624eaac5c286f20b2
///
/// Because of the limitations of DartPad, this has moved to
/// jchart_c19\C19DataSets.dart
/// C:\1d\FlutterDartPjs\JoesDemoPjs\Covid19app_jcwPjs\C19app1Pj\jchart_c19\C19DataSets.dart
///
///
/// Google: dart:html example
/// EXCELLENT:
/// 1. [[dart]Loading a web page's HTML to a string](https://stackoverflow.com/a/16937319/601770)
/// `import 'package:http/http.dart' as http; // no good`
/// 2. [Making a HTML request without dart:html](https://stackoverflow.com/a/20433342/601770)
/// "You have to use HttpClient from dart:io library."
/// 3. google: dart html client example
/// - [Quick tip: How to make HTTP requests (Dart)](https://dev.to/graphicbeacon/quick-tip-how-to-make-http-requests-dart-56dd)
///
///
/// See VERY HELPFUL ANSWERS IN [Mapping JSON into Class Objects](https://stackoverflow.com/a/45189401/601770)
/// [HTML - Using getString() to load a file](https://dart.dev/tutorials/web/fetch-data#using-getString-function)
/// google: dart await result
/// [Asynchronous programming: futures, async, await](https://dart.dev/codelabs/async-await)
///
/// [path2json cases example]('https://joecodeswell.com/cov19cty/default/selj/from-to-countyFIPS/2020-08-01/2020-08-08/6067/cases')
///
/// [html 0.14.0+4](https://pub.dev/packages/html)
///
// import 'dart:convert';
import 'dart:async'; // dartpad says: info Unused import: 'dart:async' - line 31
// import 'dart:html';
// import 'dart:io'; // Dart issue: Platform in dart:io fails #1062 "dartpad can't use dart:io ... Only servers, command-line scripts, and Flutter mobile apps can import and use dart:io.""
import 'dart:convert';
// import 'package:http/http.dart' as http; // dartpad >> NO GOOD see updates (https://github.com/dart-lang/dart-pad/issues/901)
void main() async {
// [path2json cases example]('https://joecodeswell.com/cov19cty/default/selj/from-to-countyFIPS/2020-08-01/2020-08-08/6067/cases')
// String path2portmanteaux_json = 'https://dart.dev/f/portmanteaux.json';
// String respJsonStr = await makeRequest(path2portmanteaux_json);
// print('respJsonStr: $respJsonStr');
// String path2cov19cty_json = 'https://joecodeswell.com/cov19cty/default/selj/from-to-countyFIPS/2020-08-01/2020-08-08/6067/cases';
// respJsonStr = await makeRequest(path2cov19cty_json);
// print('respJsonStr: $respJsonStr');
// respJsonStr = await makeRequest(path2cov19cty_json+'/.json');
// print('respJsonStr: $respJsonStr');
// // from https://dev.to/graphicbeacon/quick-tip-how-to-make-http-requests-dart-56dd
// var request = await HttpClient().getUrl(Uri.parse('https://swapi.co/api/people/1')); // produces a request object
// var response = await request.close(); // sends the request
// // transforms and prints the response
// await for (var contents in response.transform(Utf8Decoder())) {
// print(contents);
// }
// // RESULT: Uncaught Error: Unsupported operation: Platform._version
// // dart:io can't be used in dartpad
// // from https://api.dart.dev/stable/2.0.0/dart-io/HttpClient-class.html
// HttpClient client = new HttpClient();
// client.getUrl(Uri.parse('https://dart.dev/f/portmanteaux.json'))
// .then((HttpClientRequest request) {
// // Optionally set up headers...
// // Optionally write to the request object...
// // Then call close.
// return request.close();
// })
// .then((HttpClientResponse response) {
// print(response);
// });
print('\nSUCCESSish! Thanks, Holy Trinity.\nThis ran to the end as of: ${DateTime.now()}! \n Yahoo! \n Yahoo!\n I rest in Your Peace that Passes Understanding.');
} // end main
class C19dataSet {
/// see https://stackoverflow.com/a/45189401/601770
/// - properties can't be final
int countyFIPS;
String state;
String county;
int countyPop;
DateTime fromDate;
DateTime thruDate;
String fieldName;
List<TimeSeriesC19> valList;
C19dataSet.fromJson(Map json) {
this.countyFIPS = json['countyFIPS'];
this.state = json['state'];
this.county = json['county'];
this.countyPop = int.parse(json['countyPop']);
this.fromDate = DateTime.parse(json['fromDate']);
this.thruDate = DateTime.parse(json['thruDate']);
this.fieldName = json['fieldName'];
valList = [];
for (var itm in json['valList']) {
// print('DateStr: ${itm['DateStr']}');
DateTime dt = DateTime.parse(itm['DateStr']);
// print('dt: $dt');
int theVal;
// UNFORTUNATELY theVal fieldName VARIES in web2py can be 'ConfirmedCases' also 'Deaths'
if (json['fieldName'] == 'deaths') {
theVal = itm['Deaths'];
} else if (json['fieldName'] == 'cases') {
theVal = itm['ConfirmedCases'];
} else {
print("Houston...Problem >> json['fieldName']");
}
valList.add(new TimeSeriesC19(dt, theVal));
}
}
}
class TimeSeriesC19 {
final DateTime time;
final int value;
TimeSeriesC19(
this.time,
this.value,
);
}
// // Uses "Wrong" HttpRequest
// Future<String> makeRequest(String path2json) async {
// // const path2json = 'https://dart.dev/f/portmanteaux.json';
// String retString = '';
// try {
// // Make the GET request
// final jsonString = await HttpRequest.getString(path2json);
// // The request succeeded. Process the JSON.
// retString = processResponse(jsonString);
// } catch (e) {
// // The GET request failed. Handle the error.
// print('Couldn\'t open $path2json');
// //wordList.children.add(LIElement()..text = 'Request failed.');
// retString = 'Request failed.';
// }
// return retString;
// }
// String processResponse(String jsonString) {
// // print('jsonString: $jsonString');
// return jsonString;
// }
/*
SUCCESSish! Thanks, Holy Trinity.
This ran to the end as of: 2020-10-07 12:04:20.946!
Yahoo!
Yahoo!
I rest in Your Peace that Passes Understanding.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment