0

I'm using the exact code example of Flutter's HTTP package with just different body contents, as in:

res = await http.post(
  Uri.parse("http://localhost:3000/login"),
  headers: <String, String>{
    'Content-Type': 'application/json; charset=UTF-8',
  },
  body: jsonEncode(<String, String>{
    'email': "[email protected]",
    "password": "testPassword",
  }),
);

Yet I'm getting the error type 'String' is not a subtype of type 'Map<String, dynamic>' for the body format. And I can't leave the body without encoding to be a Map<String, String> because this will fail the API.

What should I do?

1 Answer 1

1

jsonEncode outputs a String. You can just send a Map to your API.

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

3 Comments

The problem/point is that the docs of the HTTP package has an example that doesn't work out of the box. post method expects a Map while the code passes a String. This is aside, my backend doesn't accept a Map.
May I have a look at your API docs?
I didn't write docs for it. Just a Golang API that expects JSON (works with Postman). However I solved it by adjusting the API to start accepting Maps.

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.