【发布时间】:2021-05-30 18:47:41
【问题描述】:
我提出了用户注册的发布请求。我已经关注了这个链接-https://flutter.dev/docs/cookbook/networking/send-data
但我收到以下异常 - 未处理的异常:FormatException: Invalid radix-10 number (at character 1)
方法如下-
Future<User> createUser(
String fullName, String email, String userName, String password) async {
final response = await http.post(
Uri.https('http://example.com/signup.php', 'user'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'fullname': fullName,
'email': email,
'username': userName,
'password': password
}),
);
if (response.statusCode == 201) {
s = jsonDecode(response.body);
print(s);
return User.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to create user.');
}
}
【问题讨论】:
-
当您的 api 语法错误时会发生错误,例如有时通过将 http 更改为 https 来解决
-
你打算用这条线做什么?
Uri.https('http://example.com/signup.php', 'user'),我觉得这已经够好了await http.post(http://example.com/signup.php, body: {... }); -
@JohnJoe 实施您的建议后,异常消失了。现在它正在抛出创建用户失败。
-
确保所有参数都包含值,并且url正确。
标签: flutter http exception post