]> git.cworth.org Git - lmno.games/commitdiff
Add class Player with id and name attributes
authorKevin Worth <kworth082@gmail.com>
Sun, 3 May 2020 02:36:42 +0000 (22:36 -0400)
committerCarl Worth <cworth@cworth.org>
Sat, 23 May 2020 13:49:00 +0000 (06:49 -0700)
This data model will be used when hitting the /players endpoint

See:
    git://git.cworth.org/git/empires-api/api.text
    at commit 0411bdc

flutterempires/lib/player.dart [new file with mode: 0644]

diff --git a/flutterempires/lib/player.dart b/flutterempires/lib/player.dart
new file mode 100644 (file)
index 0000000..c25a53b
--- /dev/null
@@ -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<String, dynamic> json) {
+    return Player(
+      id: json['id'],
+      name: json['name'],
+    );
+  }
+
+  static Future<Player> 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');
+    }
+  }
+}