Added fromJson, toJson
This commit is contained in:
@@ -21,4 +21,21 @@ class Game {
|
||||
String toString() {
|
||||
return 'Game{\n\tid: $id,\n\tname: $name,\n\tplayers: $players,\n\tgroup: $group,\n\twinner: $winner\n}';
|
||||
}
|
||||
|
||||
/// Creates a Game instance from a JSON object.
|
||||
Game.fromJson(Map<String, dynamic> json)
|
||||
: id = json['id'],
|
||||
name = json['name'],
|
||||
players = json['players'] != null
|
||||
? (json['players'] as List)
|
||||
.map((playerJson) => Player.fromJson(playerJson))
|
||||
.toList()
|
||||
: null,
|
||||
group = json['group'] != null ? Group.fromJson(json['group']) : null,
|
||||
winner = json['winner'] ?? '';
|
||||
|
||||
/// Converts the Game instance to a JSON object.
|
||||
String toJson() {
|
||||
return 'Game{id: $id,name: $name,players: $players,group: $group,winner: $winner}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,4 +13,17 @@ class Group {
|
||||
String toString() {
|
||||
return 'Group{id: $id, name: $name,members: $members}';
|
||||
}
|
||||
|
||||
/// Creates a Group instance from a JSON object.
|
||||
Group.fromJson(Map<String, dynamic> json)
|
||||
: id = json['id'],
|
||||
name = json['name'],
|
||||
members = (json['members'] as List)
|
||||
.map((memberJson) => Player.fromJson(memberJson))
|
||||
.toList();
|
||||
|
||||
/// Converts the Group instance to a JSON object.
|
||||
String toJson() {
|
||||
return 'Group{id: $id, name: $name,members: $members}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,4 +10,14 @@ class Player {
|
||||
String toString() {
|
||||
return 'Player{id: $id,name: $name}';
|
||||
}
|
||||
|
||||
/// Creates a Player instance from a JSON object.
|
||||
Player.fromJson(Map<String, dynamic> json)
|
||||
: id = json['id'],
|
||||
name = json['name'];
|
||||
|
||||
/// Converts the Player instance to a JSON object.
|
||||
String toJson() {
|
||||
return 'Player{id: $id,name: $name}';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user