I am trying to update my database table row using http put request in flutter web app. My backend server working fine and able to put data on database using postman app. But unable to update data from my Flutter web app. How to Create Put Request and update the data using the id ofthe row.
Getting Below Error on browser_client.dart:
unawaited(xhr.onError.first.then((_) {
// Unfortunately, the underlying XMLHttpRequest API doesn't expose any
// specific information about the error itself.
completer.completeError(
ClientException('XMLHttpRequest error.', request.url),
StackTrace.current);
}));
I am trying below Api: id is row ID in the table. company, name, address, phone are updatable variables. Passing from TextEditfields from the flutter page.
Future<Company> updatedata(
id,
company,
name,
address,
phone,
) async {
Uri url =
Uri.parse("http://--link--/$id");
var response = await http.put(url);
print(response);
headers:
<String, String>{
'Content-Type': 'application/json; charset=UTF-8',
};
body:
jsonEncode(<String, String>{
"company": company,
"name": name,
"address": address,
"phone": phone,
});
if (response.statusCode == 200) {
return Company.fromJson(json.decode(response.body));
} else {
throw Exception('Failed to update.');
}
}
CodePudding user response:
The body and header details are placed outside the request. Make sure to include that along with the request.
var response = await http.put(url, headers:
<String, String>{
'Content-Type': 'application/json; charset=UTF-8',
}, body:
jsonEncode(<String, String>{
"company": company,
"name": name,
"address": address,
"phone": phone,
});
);