0

Okay So I am confused as to why I am getting this error. I am trying to make a post request to an api endpoint. Here is my function that passes email and password data to another function that makes the api call

import 'dart:async';
import 'dart:convert';

submit() async {
var res = await LoginAPI().loginData(
    {email: _emailController.value, password: _passwordController.value});
var body = json.decode(res);
print(body);
}

Here is my function that makes the api call.

 import 'package:http/http.dart' as http;

 loginData(data) async {
var fullUrl = _url + "v1/users/login";
  return await http.post(fullUrl, body: jsonEncode(data), headers: _setHeaders());      
}

_setHeaders() => {
  'Content-Type': 'application/json',
  'Accept': 'application/json'
};

When i try to hit that endpoint on postman i get the following data

{
"success": {
  "user": {
        "id": 1,
        "first_name": "Senny",
        "last_name": "Demo",
        "bio": "I am a new bio",
        "email": "[email protected]",
        "phone_number": "081697565335",
        "default_line": "081697565335",
        "balance": 0,
        "lines": [
            {
                "id": 1,
                "user_id": 1,
                "J_Number": "081697565335",
                "account_code": "081697565335",
                "pin": "1234",
                "type": "j_number",
                "created_at": "2019-11-25 13:21:27",
                "updated_at": "2019-11-25 13:21:27"
            }
        ],
        "username": "senny_demo",
        "email_verified": true
    }
}

But on my flutter app i get the following error. Converting object to an encodable object failed: _LinkedHashMap len:2 Any help will be appreciated

1
  • what is http? and what type is res? Commented Nov 26, 2019 at 12:09

2 Answers 2

2

I see 2 problems with your code.

  1. You're passing email and password variables as keys of your Map. You should probably make them strings: {'email': _emailController.value, 'password': _passwordController.value}

  2. You're trying to decode whole Response object from json. I guess you wanted you to decode the body of it: var body = json.decode(res.body);

Sign up to request clarification or add additional context in comments.

1 Comment

Making keys the String solved my case.
2

You are trying to do jsonDecode on an Http Response. Try decoding the Response body instead and not the response with res.body. Full code below

import 'dart:async';
import 'dart:convert';

submit() async {
var res = await LoginAPI().loginData(
    {email: _emailController.value, password: _passwordController.value});
var body = json.decode(res.body);
print(body);
}

Secondly, Try putting the Keys in your map in string format like this:

{'email': _emailController.value, 'password': _passwordController.value}

1 Comment

Thanks a lot. Oversight on my part. Effect of working on three different projects in 3 different languages

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.