Skip to content

Exception has occurred. _TypeError (type 'ApiResponse' is not a subtype of type 'int?') #339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 28, 2025

Conversation

DigitalSeneca
Copy link
Contributor

No description provided.

@DigitalSeneca DigitalSeneca requested a review from epou March 28, 2025 08:45
@epou
Copy link
Member

epou commented Mar 28, 2025

Why not change the createSession to return Future<int?>?

Future<int?> createSession(Session session) async {
    try {
      final response = await http
          .post(Uri.parse('$serverUrl$sessions'),
              headers: headers,
              body: json.encode(
                session.toJson(),
              ))
          .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}");
          }
    } on TimeoutException {
      print("Error: Request timed out.");
    } on http.ClientException catch (e) {
      print("HTTP Client Exception: $e");
    } catch (e) {
      print("Error : ${e}");
    }
    return null;
  }

So then the Utils.createNewReports is simplified into:

Session session = Session(
          session_ID: sessionId,
          user: userUUID,
          session_start_time: DateTime.now().toUtc().toIso8601String());

print(language);
session.id = await ApiSingleton().createSession(session);

What do you think?

@DigitalSeneca
Copy link
Contributor Author

I agree, I've applied your changes, except the line

Session session = Session(

because this would cause a bug, since session is not a new variable but a static class variable

@@ -92,13 +92,13 @@ class Utils {

int? sessionId = response + 1;

session = Session(
Session session = Session(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

already defined on top

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry I hadn't pushed my last commit

@DigitalSeneca DigitalSeneca requested a review from epou March 28, 2025 12:33
@DigitalSeneca DigitalSeneca added this pull request to the merge queue Mar 28, 2025
Merged via the queue into main with commit b90df5f Mar 28, 2025
3 checks passed
@DigitalSeneca DigitalSeneca deleted the fix-exception branch March 28, 2025 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants