Created
October 28, 2021 15:54
-
-
Save teerasej/8bf78de8e3f3e3c6be0824515eeed32a to your computer and use it in GitHub Desktop.
Thailand's covid stat daily model class with support non-nullable in Flutter 2.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To parse this JSON data, do | |
// | |
// final covidStatModel = covidStatModelFromJson(jsonString); | |
import 'dart:convert'; | |
List<CovidStatModel> covidStatModelFromJson(String str) => | |
List<CovidStatModel>.from( | |
json.decode(str).map((x) => CovidStatModel.fromJson(x))); | |
String covidStatModelToJson(List<CovidStatModel> data) => | |
json.encode(List<dynamic>.from(data.map((x) => x.toJson()))); | |
class CovidStatModel { | |
CovidStatModel({ | |
required this.txnDate, | |
required this.newCase, | |
required this.totalCase, | |
required this.newCaseExcludeabroad, | |
required this.totalCaseExcludeabroad, | |
required this.newDeath, | |
required this.totalDeath, | |
required this.newRecovered, | |
required this.totalRecovered, | |
required this.updateDate, | |
}); | |
DateTime? txnDate; | |
int? newCase; | |
int? totalCase; | |
int? newCaseExcludeabroad; | |
int? totalCaseExcludeabroad; | |
int? newDeath; | |
int? totalDeath; | |
int? newRecovered; | |
int? totalRecovered; | |
DateTime? updateDate; | |
factory CovidStatModel.fromJson(Map<String, dynamic> json) => CovidStatModel( | |
txnDate: | |
json["txn_date"] == null ? null : DateTime.parse(json["txn_date"]), | |
newCase: json["new_case"] == null ? null : json["new_case"], | |
totalCase: json["total_case"] == null ? null : json["total_case"], | |
newCaseExcludeabroad: json["new_case_excludeabroad"] == null | |
? null | |
: json["new_case_excludeabroad"], | |
totalCaseExcludeabroad: json["total_case_excludeabroad"] == null | |
? null | |
: json["total_case_excludeabroad"], | |
newDeath: json["new_death"] == null ? null : json["new_death"], | |
totalDeath: json["total_death"] == null ? null : json["total_death"], | |
newRecovered: | |
json["new_recovered"] == null ? null : json["new_recovered"], | |
totalRecovered: | |
json["total_recovered"] == null ? null : json["total_recovered"], | |
updateDate: json["update_date"] == null | |
? null | |
: DateTime.parse(json["update_date"]), | |
); | |
Map<String, dynamic> toJson() => { | |
"txn_date": txnDate == null | |
? null | |
: "${txnDate?.year.toString().padLeft(4, '0')}-${txnDate?.month.toString().padLeft(2, '0')}-${txnDate?.day.toString().padLeft(2, '0')}", | |
"new_case": newCase == null ? null : newCase, | |
"total_case": totalCase == null ? null : totalCase, | |
"new_case_excludeabroad": | |
newCaseExcludeabroad == null ? null : newCaseExcludeabroad, | |
"total_case_excludeabroad": | |
totalCaseExcludeabroad == null ? null : totalCaseExcludeabroad, | |
"new_death": newDeath == null ? null : newDeath, | |
"total_death": totalDeath == null ? null : totalDeath, | |
"new_recovered": newRecovered == null ? null : newRecovered, | |
"total_recovered": totalRecovered == null ? null : totalRecovered, | |
"update_date": | |
updateDate == null ? null : updateDate?.toIso8601String(), | |
}; | |
} |
URL for this JSON:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Convert
http
response's data to Dart object with model class generated from Quicktype.io