import 'dart:developer'; import 'package:flutter/material.dart'; import 'package:mchess/api/register.dart'; import 'package:mchess/pages/chess_game.dart'; import 'package:http/http.dart' as http; import 'dart:convert'; class PrepareChessGameWidget extends StatelessWidget { const PrepareChessGameWidget({super.key}); @override Widget build(BuildContext context) { return FutureBuilder( future: registerForRandomGame(), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.done) { log('future done ${snapshot.data?.playerID}'); return ChessGame( playerID: snapshot.data!.playerID, lobbyID: snapshot.data!.lobbyID, ); } return const CircularProgressIndicator(); }, ); } Future registerForRandomGame() async { final response = await http.get( Uri.parse('http://localhost:8080/api/random'), headers: {"Accept": "application/json"}); if (response.statusCode == 200) { log(response.body); return ResponseFromRegisteringGame.fromJson(jsonDecode(response.body)); } else { // If the server did not return a 200 OK response, // then throw an exception. throw Exception('Failed to register for random game'); } } }