Mohammad Faizan
Mohammad Faizan

Reputation: 864

How to make json request in flutter using https

Error: E/flutter (20161): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: type '_InternalLinkedHashMap<String, String>' is not a subtype of type 'String' in type cast

My API code below_

Please check my code and correct me where I am wrong.

Future<LoginResponse?> submitData(
    String email, String password, String role) async {
  var response =
      await http.post(Uri.https('3.20.233.00', 'user/signIn'), body: {
    "email": email,
    "password": password,
    "role": {"name": role}
  });
  var data = response.body;
  print(data);
  if(response.statusCode == 200){
    String responseString = response.body;
    loginResponseFromJson(responseString);
  }
  else {
    return null;
  }
} ```

**
Error find in my request

when I run in a debug mode the show me _InternalLinkedHashMap in role type.

E/flutter (20161): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: type '_InternalLinkedHashMap<String, String>' is not a subtype of type 'String' in type cast
**

Upvotes: -1

Views: 62

Answers (4)

Mohammad Faizan
Mohammad Faizan

Reputation: 864

Future<LoginResponse?> submitData(
String email, String password, String role) async {
  var body = json.encode({
    "email": email,
    "password": password,
    "role": {"name": role}
  });
  var response =
      await http.post(Uri.https('3.20.233.00', 'user/signIn'), body: body);
  var data = response.body;
  print(data);
  if(response.statusCode == 200){
    String responseString = response.body;
    loginResponseFromJson(responseString);
  }
  else {
    return null;
  }
}

Upvotes: 0

aditya
aditya

Reputation: 184

just change the type of responseString to just Map or Map<String,dynamic>

Future<LoginResponse?> submitData(
    String email, String password, String role) async {
  var response =
      await http.post(Uri.https('3.20.233.00', 'user/signIn'), body: {
    "email": email,
    "password": password,
    "role": {"name": role}
  });
  var data = response.body;
  print(data);
  if(response.statusCode == 200){
    Map<String,dynamic>responseString = response.body;
    loginResponseFromJson(responseString);
  }
  else {
    return null;
  }
} 

Upvotes: 0

Allan Mitre
Allan Mitre

Reputation: 321

try encoding the body before post:

http.post(
    Uri.parse('https://...'),
body: jsonEncode(<String, String>{
      'title': title,
    })

another posible problem could be in your function loginResponseFromJson(responseString); make sure you are decoding the response body correctly.

Upvotes: 0

Hardik Mehta
Hardik Mehta

Reputation: 2415

You have to wrap your body with jsonEncode()

import 'dart:convert';
Future<LoginResponse?> submitData(
    String email, String password, String role) async {
  var response =
      await http.post(Uri.https('3.20.233.00', 'user/signIn'), body: jsonEncode({
    "email": email,
    "password": password,
    "role": {"name": role}
  }));
  var data = response.body;
  print(data);
  if(response.statusCode == 200){
    String responseString = response.body;
    loginResponseFromJson(responseString);
  }
  else {
    return null;
  }
}

Upvotes: 0

Related Questions