Home > Enterprise >  How to make json request in flutter using https
How to make json request in flutter using https

Time:08-25

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

CodePudding user response:

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

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

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.235.26', 'user/signIn'), body: body);
  var data = response.body;
  print(data);
  if(response.statusCode == 200){
    String responseString = response.body;
    loginResponseFromJson(responseString);
  }
  else {
    return null;
  }
}
  • Related