Skip to content

Commit b90df5f

Browse files
Exception has occurred. _TypeError (type 'ApiResponse' is not a subtype of type 'int?') (#339)
* Exception has occurred. _TypeError (type 'ApiResponse' is not a subtype of type 'int?') * Fix bug session is not accessible
1 parent 886d776 commit b90df5f

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

Diff for: lib/api/api.dart

+14-18
Original file line numberDiff line numberDiff line change
@@ -217,34 +217,30 @@ class ApiSingleton {
217217
}
218218
}
219219

220-
Future<dynamic> createSession(Session session) async {
220+
Future<int?> createSession(Session session) async {
221221
try {
222222
final response = await http
223223
.post(Uri.parse('$serverUrl$sessions'),
224224
headers: headers,
225225
body: json.encode(
226226
session.toJson(),
227227
))
228-
.timeout(
229-
Duration(seconds: _timeoutTimerInSeconds),
230-
onTimeout: () {
231-
print('Request timed out');
232-
return Future.error('Request timed out');
233-
},
234-
);
235-
;
236-
237-
if (response.statusCode != 201) {
238-
print(
239-
'Request: ${response.request.toString()} -> Response: ${response.body}');
240-
return ApiResponse.fromJson(json.decode(response.body));
228+
.timeout(Duration(seconds: _timeoutTimerInSeconds));
229+
if (response.statusCode == 201) {
230+
var body = json.decode(response.body);
231+
return body['id'] as int;
232+
} else {
233+
print("Error: Unexpected response code ${response.statusCode}");
234+
print("Response body: ${response.body}");
241235
}
242-
243-
var body = json.decode(response.body);
244-
return body['id'];
236+
} on TimeoutException {
237+
print("Error: Request timed out.");
238+
} on http.ClientException catch (e) {
239+
print("HTTP Client Exception: $e");
245240
} catch (e) {
246-
return null;
241+
print("Error : ${e}");
247242
}
243+
return null;
248244
}
249245

250246
Future<dynamic> closeSession(Session session) async {

Diff for: lib/utils/Utils.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class Utils {
101101
session!.id = await ApiSingleton().createSession(session!);
102102
}
103103

104-
if (session!.id != null) {
104+
if (session?.id != null) {
105105
var lang = await UserManager.getLanguage();
106106
var userUUID = await UserManager.getUUID();
107107
report = Report(

0 commit comments

Comments
 (0)