我正在嘗試在 Flutter Web 應用程式中使用 http put 請求更新我的資料庫表行。我的后端服務器作業正常,并且能夠使用郵遞員應用程式將資料放入資料庫。但無法從我的 Flutter Web 應用程式更新資料。如何創建 Put Request 并使用行的 id 更新資料。
在 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);
}));
我在 Api 下面嘗試: id 是表中的行 ID。公司、姓名、地址、電話是可更新的變數。從 Flutter 頁面中的 TextEditfields 傳遞。
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.');
}
}
uj5u.com熱心網友回復:
正文和標頭詳細資訊放置在請求之外。確保將其與請求一起包含在內。
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,
});
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/427689.html
