我正在嘗試通過 POST http 請求向服務器發送一個包含三個引數的串列。回應狀態為 200,但是當我列印回應正文為空時,它會回圈列印它。我無法弄清楚我做錯了什么。任何幫助都感激不盡!
POST 請求
static Future<Athlete> insertPresences(
int athleteId, int departmentId, int teamId) async {
var body = [
{"athleteId": athleteId, "departmentId": departmentId, "teamId": teamId}
];
try {
final response =
await http.post(Uri.parse('$uri/nox/api/insert-presences'),
headers: <String, String>{
'Authorization': 'Basic a2F4cmlzOjEyMzQ1',
'Content-Type': 'application/json; charset=UTF-8',
'Accept': 'application/json'
},
body: json.encode(body),
);
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');
if (response.statusCode == 200) {
if (response.body.isNotEmpty) {
return Athlete.fromJson(jsonDecode(response.body));
}
}
} catch (e) {
print(e);
}
return insertPresences(athleteId, departmentId, teamId);
}
郵遞員原始身體
[
{
"athleteId":"16198",
"departmentId":"3",
"teamId":"278"
}
]
它列印什么
flutter: Response status: 200
flutter: Response body:
flutter: Response status: 200
flutter: Response body:
flutter: Response status: 200
flutter: Response body:
它一直持續到我停止它
uj5u.com熱心網友回復:
您的回應正文為空,因此它不會跳入您的 if
if (response.body.isNotEmpty) {
return Athlete.fromJson(jsonDecode(response.body)); // NOT CALLED
}
但是最后一個 return 被執行了,這會創建一個回圈,因為你在 return 中再次呼叫了 insertPresences 方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/523347.html
標籤:扑镖
下一篇:在顫動中將字串日期轉換為日期時間
