diff --git a/lib/api/api.dart b/lib/api/api.dart index e9a9e961..d2ff3cc1 100644 --- a/lib/api/api.dart +++ b/lib/api/api.dart @@ -217,7 +217,7 @@ class ApiSingleton { } } - Future createSession(Session session) async { + Future createSession(Session session) async { try { final response = await http .post(Uri.parse('$serverUrl$sessions'), @@ -225,26 +225,22 @@ class ApiSingleton { body: json.encode( session.toJson(), )) - .timeout( - Duration(seconds: _timeoutTimerInSeconds), - onTimeout: () { - print('Request timed out'); - return Future.error('Request timed out'); - }, - ); - ; - - if (response.statusCode != 201) { - print( - 'Request: ${response.request.toString()} -> Response: ${response.body}'); - return ApiResponse.fromJson(json.decode(response.body)); + .timeout(Duration(seconds: _timeoutTimerInSeconds)); + if (response.statusCode == 201) { + var body = json.decode(response.body); + return body['id'] as int; + } else { + print("Error: Unexpected response code ${response.statusCode}"); + print("Response body: ${response.body}"); } - - var body = json.decode(response.body); - return body['id']; + } on TimeoutException { + print("Error: Request timed out."); + } on http.ClientException catch (e) { + print("HTTP Client Exception: $e"); } catch (e) { - return null; + print("Error : ${e}"); } + return null; } Future closeSession(Session session) async { diff --git a/lib/utils/Utils.dart b/lib/utils/Utils.dart index 102139ca..4d2d23e2 100644 --- a/lib/utils/Utils.dart +++ b/lib/utils/Utils.dart @@ -101,7 +101,7 @@ class Utils { session!.id = await ApiSingleton().createSession(session!); } - if (session!.id != null) { + if (session?.id != null) { var lang = await UserManager.getLanguage(); var userUUID = await UserManager.getUUID(); report = Report(