From: Kevin Worth Date: Sun, 3 May 2020 02:36:42 +0000 (-0400) Subject: Add class Player with id and name attributes X-Git-Url: https://git.cworth.org/git?p=lmno.games;a=commitdiff_plain;h=0e791b7181f2893b3b5be19f3a2b21f135debbb9 Add class Player with id and name attributes This data model will be used when hitting the /players endpoint See: git://git.cworth.org/git/empires-api/api.text at commit 0411bdc --- diff --git a/flutterempires/lib/player.dart b/flutterempires/lib/player.dart new file mode 100644 index 0000000..c25a53b --- /dev/null +++ b/flutterempires/lib/player.dart @@ -0,0 +1,28 @@ +import 'dart:convert'; +import 'dart:js'; +import 'package:http/http.dart' as http; + +class Player { + final int id; + final String name; + + Player({this.id, this.name}); + + factory Player.fromJson(Map json) { + return Player( + id: json['id'], + name: json['name'], + ); + } + + static Future fetchPlayer() async { + final response = await http.get('https://families.cworth.org/api/players'); + + if (response.statusCode == 200) { + JsArray playerList = JsArray.from(json.decode(response.body)); + return Player.fromJson(playerList.elementAt(0)); + } else { + throw Exception('Failed to load player'); + } + } +}