In POST API call am getting this error "Exception has occurred. _TypeError (type 'String' is not a subtype of type 'Map<String, dynamic>')"

Viewed 195

In POST API call am getting this error "Exception has occurred. _TypeError (type 'String' is not a subtype of type 'Map<String, dynamic>')". here is the code of mine:

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 Calling:

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.');
    }
  }

and am getting this error

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

can Any one help me from this?

1 Answers

Try this URL call this will help

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

Related