SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 37312 while http post

Viewed 1497

I'm trying to post data to node API from flutter. my code is as follows.

final String uri = "http://localhost:3000";
Map<String, String> userBody = {
"firstName":"Vishal",
"lastName":"Soni",
"email":"vishal@gmail.com",
"mobile":"1234567890",
"password":"123",
};

Future<dynamic> postData() async {
try {
  http.Response res = await http.Client().post("$uri/register", body: jsonEncode(userBody), headers:    {
    'Content-Type': 'application/json',
  });
  print(res);
} catch (e) {
  print(e);
 }
}

Getting following error enter image description here

2 Answers

You need to change the localhost. It should be your IP address.

     final String uri = "http://10.0.2.2:3000";

Look here for the same issue on Github.

run the below command in the terminal

adb reverse tcp:3000 tcp:3000 
Related