Error when trying to do a HTTP Post request in Flutter


I am trying to do an HTTP Post request, which used to work, but all of a sudden (I suppose some updates) it does not work anymore. Here it is the function:

Future<int> postQuestion(User userOwner, User userAsked, String questionText, int qoinsTotal, int votes) async {
var dateAndTime = new;
Map<String, String> headers = {"Content-type": "application/json", "Authorization": "Bearer $_token"};
String jsonDoc = '{"UserOwner": "${userOwner.username}", "UserAsked": "${userAsked.username}", "UserAskedId": ${userAsked.userId}, '
            '"QuestionText": "$questionText", "QoinsTotal": $qoinsTotal, "Votes": $votes, "DateAndTime": "$dateAndTime"}';

var uriResponse = await"question/${userOwner.userId}", headers: headers, body: jsonDoc);
return uriResponse.statusCode; //It must be 201


I get the following error:
[ERROR:flutter/lib/ui/] Unhandled Exception: type ‘(HttpException) => Null’ is not a subtype of type ‘(dynamic) => dynamic’

The issue is that the request goes through (the API it communicates with receives the request and it does what is it supposed to do), so I am not sure why the error appears.

I have no idea what to do, I am really lost.

Thank you in advance.


I have solved the issue. The problem was the returned json document, which was incorrect (thus, the error, as practically it received an incorrect json document i.e. the json document did not finish with a ‘}’).

Answered By – Vlad Hondru

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *