在 POST API 调用中,收到此错误“发生异常。_TypeError(类型“String”不是类型“Map”的子类型)”

2024-05-01

在 POST API 调用中,我收到此错误“发生异常。_TypeError(类型“String”不是类型“Map”的子类型)”。这是我的代码:

model:

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();
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 POST API 调用中,收到此错误“发生异常。_TypeError(类型“String”不是类型“Map”的子类型)” 的相关文章

随机推荐