mchess-client/lib/pages/prepare_chess_game.dart

42 lines
1.2 KiB
Dart
Raw Normal View History

import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:mchess/models/models.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,
);
}
return const CircularProgressIndicator();
},
);
}
Future<ResponseFromRegisteringGame> registerForRandomGame() async {
final response =
await http.get(Uri.parse('http://localhost:8080/api/random'));
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 load album');
}
}
}