Lee Maan
Lee Maan

Reputation: 719

Why I'm getting Flutter HTTP package error type 'String' is not a subtype of type 'Map<String, dynamic>'?

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?

Upvotes: 0

Views: 67

Answers (1)

Mudassir
Mudassir

Reputation: 806

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

Upvotes: 1

Related Questions