【发布时间】:2021-05-21 19:36:21
【问题描述】:
我需要从 API 获取数据,但我的 try-catch 失败并出现 SyntaxError: Unexpected token
代码:
String problems;
try {
final response = await http.get('http://myURL',headers: {"Accept": "application/json"});
log("after http");
if (response.statusCode == 200) {
final data = json.decode(response.body);
problems = data["IsSuccess"];
return problems.toString();
} else {
throw Exception('Failed to load status data');
}
} catch (exc) {
log("catch:" + exc.toString());
}
}
【问题讨论】:
-
您收到错误是因为您的 api 返回的是 html 而不是 json。
标签: json api flutter http dart