]> git.cworth.org Git - lmno.games/blobdiff - flutterempires/lib/main.dart
Add initState to start the async call to fetch player
[lmno.games] / flutterempires / lib / main.dart
index 7bb1c7deb4d75311e97d82f1a959cde946f122f2..816b13eab8ad4082d493bcb9b901e84bdc946c53 100644 (file)
@@ -53,6 +53,14 @@ class MyHomePage extends StatefulWidget {
 class _MyHomePageState extends State<MyHomePage> {
   int _counter = 0;
 
+  Future<Player> futurePlayer;
+
+  @override
+  void initState() {
+    super.initState();
+    futurePlayer = Player.fetchPlayer();
+  }
+
   void _incrementCounter() {
     setState(() {
       // This call to setState tells the Flutter framework that something has
@@ -107,7 +115,13 @@ class _MyHomePageState extends State<MyHomePage> {
               ),
               Spacer(flex: 1),
               FutureBuilder<Player>(
+                future: futurePlayer,
                 builder: (context, snapshot) {
+                  if (snapshot.hasData) {
+                    return Text(snapshot.data.name);
+                  } else if (snapshot.hasError) {
+                    return Text("${snapshot.error}");
+                  }
                   // By default, show a loading spinner.
                   return CircularProgressIndicator();
                 },