Flutter SocketException (SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 51500)

Sagar Shende picture Sagar Shende · Jan 31, 2020 · Viewed 13.1k times · Source

I am Using strapi.io open Source Node.js Headless CMS

this is my code for registerpage

void _registerUser() async {

http.Response response =
    await http.post('http://localhost:1337/auth/local/register', body: {
  "username": _userName,
  "email": _email,
  "password": _password,
});
final responseData = json.decode(response.body);

if (response.statusCode == 200) {
  // If the call to the server was successful, parse the JSON
  return print(responseData);
} else {
  // If that call was not successful, throw an error.
  throw Exception('Failed to load post');
}

}

any idea how to solve this

Problem about Socket exception

update:

Found the Solution: If you're running the server locally and using the Android emulator, then your server endpoint should be 10.0.2.2:8000 instead of localhost:8000 as AVD uses 10.0.2.2 as an alias to your host loopback interface (i.e) localhost

clear explanation checkout this Answer:

SocketException: OS Error: Connection refused, errno = 111 in flutter using django backend

Thank you So much Everyone For Helping me!!❤❤❤

Answer

Mohamed Reda picture Mohamed Reda · Jul 27, 2020

1 open cmd

2 run ipconfig at cmd

3 copy IPv4 Address from cmd

4 at 'http://localhost:1337/auth/local/register' replace 'localhost' with your IPv4 Address

5 run your app