我发送请求,但有一个例外 这是模型类:
class User{
String mobileId , username;
User({this.username,this.mobileId});
factory User.fromJson(Map<String, dynamic> json){
return User(
username: json['userName'],
mobileId: json['mobileId'],
);
}
}
这是我发送请求的功能
Future<User> sendRequest(String mobileId , String username)async{
http.Response response = await http.post('http://94.237.88.194:8080/player',
body:jsonEncode(<String,String>{
'userName' : username,
'mobileId' : mobileId,
}),
);
print(response.statusCode);
return User.fromJson(jsonDecode(response.body));
}
答案 0 :(得分:2)
我找到了解决方案 我在发布函数中添加了标题
Map<String, String> header = {"Content-Type": "application/json"};
函数变为:
Future<User> sendRequest(String mobileId , String username)async{
print('5');
Map<String, String> header = {"Content-Type": "application/json"};
http.Response response = await http.post('http://94.237.88.194:8080/player',headers: header,
body:jsonEncode(<String,String>{
'userName' : username,
'mobileId' : mobileId,
}),
);
print('4');
print(response.statusCode);
print(response.body);
return User.fromJson(jsonDecode(response.body));
}