javiercbk / json_to_dart

Library that generates dart classes from json strings
https://javiercbk.github.io/json_to_dart/
The Unlicense
1.33k stars 371 forks source link

Wrong Type generated #87

Open kimvnhung opened 2 months ago

kimvnhung commented 2 months ago

My sample json { "ID": 1, "Account": { "ID": 1, "Username": "username", "Password": "123", "CredenticalID": 1 }, "FacebookAuthentication": { "ID": 1, "Name": "fbName", "Token": "1213", "CredenticalID": 1 }, "GoogleAuthentication": { "ID": 1, "Name": "ggName", "Token": "1213", "CredenticalID": 1 }, "Guest": { "ID": 1, "GuestId": "GuestId", "CredenticalID": 1 }, "UserID": 1 }

The result: _class Credentical { int? iD; Account? account; FacebookAuthentication? facebookAuthentication; FacebookAuthentication? googleAuthentication; Guest? guest; int? userID;

Credentical( {this.iD, this.account, this.facebookAuthentication, this.googleAuthentication, this.guest, this.userID});

Credentical.fromJson(Map<String, dynamic> json) { iD = json['ID']; account = json['Account'] != null ? new Account.fromJson(json['Account']) : null; facebookAuthentication = json['FacebookAuthentication'] != null ? new FacebookAuthentication.fromJson(json['FacebookAuthentication']) : null; googleAuthentication = json['GoogleAuthentication'] != null ? new FacebookAuthentication.fromJson(json['GoogleAuthentication']) : null; guest = json['Guest'] != null ? new Guest.fromJson(json['Guest']) : null; userID = json['UserID']; }

Map<String, dynamic> toJson() { final Map<String, dynamic> data = new Map<String, dynamic>(); data['ID'] = this.iD; if (this.account != null) { data['Account'] = this.account!.toJson(); } if (this.facebookAuthentication != null) { data['FacebookAuthentication'] = this.facebookAuthentication!.toJson(); } if (this.googleAuthentication != null) { data['GoogleAuthentication'] = this.googleAuthentication!.toJson(); } if (this.guest != null) { data['Guest'] = this.guest!.toJson(); } data['UserID'] = this.userID; return data; } }

class Account { int? iD; String? username; String? password; int? credenticalID;

Account({this.iD, this.username, this.password, this.credenticalID});

Account.fromJson(Map<String, dynamic> json) { iD = json['ID']; username = json['Username']; password = json['Password']; credenticalID = json['CredenticalID']; }

Map<String, dynamic> toJson() { final Map<String, dynamic> data = new Map<String, dynamic>(); data['ID'] = this.iD; data['Username'] = this.username; data['Password'] = this.password; data['CredenticalID'] = this.credenticalID; return data; } }

class FacebookAuthentication { int? iD; String? name; String? token; int? credenticalID;

FacebookAuthentication({this.iD, this.name, this.token, this.credenticalID});

FacebookAuthentication.fromJson(Map<String, dynamic> json) { iD = json['ID']; name = json['Name']; token = json['Token']; credenticalID = json['CredenticalID']; }

Map<String, dynamic> toJson() { final Map<String, dynamic> data = new Map<String, dynamic>(); data['ID'] = this.iD; data['Name'] = this.name; data['Token'] = this.token; data['CredenticalID'] = this.credenticalID; return data; } }

class Guest { int? iD; String? guestId; int? credenticalID;

Guest({this.iD, this.guestId, this.credenticalID});

Guest.fromJson(Map<String, dynamic> json) { iD = json['ID']; guestId = json['GuestId']; credenticalID = json['CredenticalID']; }

Map<String, dynamic> toJson() { final Map<String, dynamic> data = new Map<String, dynamic>(); data['ID'] = this.iD; data['GuestId'] = this.guestId; data['CredenticalID'] = this.credenticalID; return data; } }_

googleAuthentication field in Credentical class was generated with wrong type