Search code examples
flutterflutter-webflutter-test

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
**

Solution

  • 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;
      }
    }