Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

strapi

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!!❤❤❤

like image 346
Sagar Shende Avatar asked Jan 31 '20 09:01

Sagar Shende


2 Answers

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

like image 174
Mohamed Reda Avatar answered Oct 11 '22 04:10

Mohamed Reda


Instead of localhost, put your machine IP address. Mobile doesnt understand localhost.

like image 22
Amit Sinha Avatar answered Oct 11 '22 06:10

Amit Sinha