I'm using universal html package which is same as the dart:html package but work with multiple platforms. From that I'm making this http POST request to my local XAMPP server.
Future newConn() async {
try {
await html.HttpRequest.postFormData(
'http://192.166.6.150/test.php',
{'firstName': 'John', 'lastName': 'Doe'},
).then(
(value) => print("completed$value"),
);
;
} on Exception {
print("error");
}
}
In here $value is the httpResponse object. But when I run the above script flutter gives me "Instance of 'ProgressEvent' " error. I have tried this on my local emulator and chrome both. My php is as below.
test.php
<?php
$nameF = $_POST["firstName"];
$nameSec = $_POST["lastName"];
echo $nameF;
exit();
?>
CodePudding user response:
It's a bug in dart:html
. Take a look at the reported issue on Github. There is a workaround to it in the post here. The workaround is as follows:
Check if isNetworkImageUnauthorizedError(value)
returns true
. If so, it means value
is an error:
bool isNetworkImageUnauthorizedError(dynamic error) {
if (error is NetworkImageLoadException && error.statusCode == 401) {
return true;
}
//Because of this bug, we need to analyze the error for ProgressEvent
//https://github.com/flutter/flutter/issues/44370
//https://github.com/flutter/flutter/issues/60239
if(error is html.ProgressEvent && error.currentTarget != null) {
if (error.currentTarget is html.HttpRequest) {
html.HttpRequest request = error.currentTarget as html.HttpRequest;
if (request.status == 401) {
return true;
}
}
}
return false;
}