我正在使用DART的http包发出POST请求.由于某些服务器发出其抛出异常.我已经将代码包装在一个try catch挡路代码中,但它没有捕获异常.
以下是发出网络请求的代码
class VerificationService {
static Future<PhoneVerification> requestOtp(
PhoneNumberPost phoneNumberPostData) async {
final String postData = jsonEncode(phoneNumberPostData);
try {
final http.Response response = await http.post(
getPhoneRegistrationApiEndpoint(),
headers: {'content-type': 'Application/json'},
body: postData,
);
if(response.statusCode == 200) {
return PhoneVerification.fromJson(json.decode(response.body));
} else {
throw Exception('Request Error: ${response.statusCode}');
}
} on Exception {
rethrow;
}
}
}
使用上述静电方法的单独类的函数.
void onButtonClick() {
try {
VerificationService.requestOtp(PhoneNumberPost(phone))
.then((PhoneVerification onValue) {
//Proceed to next screen
}).catchError((Error onError){
enableInputs();
});
} catch(_) {
print('WTF');
}
}
在上面的方法中,永远不会捕获异常.控制台上从不打印"WTF".我在这里做错了什么?我是dart 新手.