Home > Software engineering >  type '_InternalLinkedHashMap<String, String>' is not a subtype of type '((Objec
type '_InternalLinkedHashMap<String, String>' is not a subtype of type '((Objec

Time:08-04

I am performing a post request and after filling the form, I get this

InternalLinkedHashMap<String, String>' is not a subtype of type '((Object?, Object?) => Object?)? error.

This controller contains my post method

class PostController extends GetxController {
final String url = ProjectApis.requestUrl;
ProjectApis _projectApis = ProjectApis();

Client _client = Client();

postRequest(userData, url) async {
  var response = await _client. Post(
    Uri.parse(url),
    body: JsonDecoder(userData),
    headers: _projectApis.headers,
  );
  return response;
}

}

This is the data I am sending across after a user fills a form

sendUserRequest() async {

var userData = {
  'title': _titleController.text,
  'author': _authorController.text,
  'index': _indexController.text,
  'date': _dateController.text,
  'return': _returnController.text,
  'name': _nameController.text,
};

var response =
    await _postController.postRequest(userData, ProjectApis.requestUrl);

var body = json.decode(response.body);
if (body['success']) {
  _clearForm();
  Get.snackbar(
    'Success',
    'Your request has been sent successfully',
    duration: Duration(seconds: 2),
  );
} else {
  Get.snackbar(
    'Error',
    'Something went wrong,
    duration: Duration(seconds: 2),
  );
}

}

And the error message is

_TypeError (type '_InternalLinkedHashMap<String, String>' is not a subtype of type '((Object?, Object?) => Object?)?')

The code breaks on (userData)

I have read this enter link description here but it does not seem to solve my problem please.

CodePudding user response:

Here it's decoded instead of being encoded

postRequest(userData, url) async {
  var response = await _client. Post(
    Uri.parse(url),
    body: JsonDecoder(userData), // issue here
    headers: _projectApis.headers,
  );
  return response;
}

Please change jsonDecoder to encoder like the following

postRequest(userData, url) async {
  var response = await _client. Post(
    Uri.parse(url),
    body: json.encode(userData), //change it to encoder
    headers: _projectApis.headers,
  );
  return response;
}
  • Related