【问题标题】:Flutter Http post request is throwing Unhandled Exception: FormatException: Invalid radix-10 number (at character 1)Flutter Http post请求抛出未处理的异常:FormatException:无效的radix-10数字(在字符1处)
【发布时间】: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


【解决方案1】:

我必须修改我的方法才能使其正常工作。

这是代码-

Future<User> createUser(
  String fullName, String email, String userName, String password) async {
final response = await http.post('https://andoirdtvapp.hiphopnblog.com/signup.php',
  
  body: {
    'fullname' : fullName,
    'email' : email,
    'username' : userName,
    'password' : password,
  },
);
  s = response.body;
  return User.fromJson(jsonDecode(response.body));
  }

【讨论】:

    猜你喜欢
    • 2021-04-07
    • 2021-06-11
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    相关资源
    最近更新 更多