在POST API调用中,我得到了这个错误";出现异常_TypeError(类型'String'



在POST API调用中,我得到这个错误";出现异常_TypeError(类型"String"不是类型"Map<String,dynamic>"的子类型("。这是我的代码:

型号:

class User{
String name;
String emailId;
String password;
User({this.name,this.emailId,this.password});
factory User.fromJson(Map<String, dynamic> responseData){
return User(
name: responseData['name'],
emailId: responseData['email'],
password: responseData['password'],
);
}
}

API调用:

Future<User> createAlbum() async {
setState(() {
indicator = true;
});
final response = await http.post(
Uri.parse(registerUrl),
body: jsonEncode(<String, String>{
'name': name.text,
'email': email.text,
'password': password.text
}),
);
print(response.body);
if (response.statusCode == 200) {
setState(() {
indicator = false;
});
return User.fromJson(json.decode(response.body));
} else {
throw Exception('Failed to create album.');
}
}

我收到这个错误

Exception has occurred. _TypeError (type 'String' is not a subtype of type 'Map<String, dynamic>')

有人能帮我吗?

尝试此URL调用,这将有助于

Future<List<User>> createAlbum() async {
final response = await http.post(
Uri.parse(registerUrl),
body: jsonEncode(<String, String>{
'name': name.text,
'email': email.text,
'password': password.text
}),
);
print(responce.body.toString());
return parseuser(responce.body);
}
List<Newpop> parseuser(String responceBody) {
final parsed = jsonDecode(responceBody).cast<Map<String, dynamic>>();
print(responceBody);
return parsed.map<User>((json) => User.fromjson(json)).toList();
}

最新更新