Sagar Shende
Sagar Shende

Reputation: 271

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

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

Upvotes: 13

Views: 36724

Answers (7)

Anandh Krishnan
Anandh Krishnan

Reputation: 5984

For MAC Users

Instead of localhost, put your MAC IP address. Mobile will not understand localhost.

Click

apple logo > system prefrences > Network

Under connected networks you can find your wifi ip

after that instead of

http://localhost:1337

you can use your wifi ip

http://192.168.2.250

enter image description here

Upvotes: 3

Liliana J
Liliana J

Reputation: 549

I had de same issue, it was caused because I builded an app version with signed on debug mode, on Android Studio, it was ok. But later on when I tried to run on the app on visual studio the error appears!!, so I went to Android Studio in normal mode, and runned the app , the error desapears. Because these error only appeeared in Android versión.

Upvotes: 0

Mohamed Reda
Mohamed Reda

Reputation: 1607

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

Upvotes: 20

Mohammad Awais
Mohammad Awais

Reputation: 93

Worked for me!! Insert your local IP address (192.168.x.x) instead of localhost. You can find it by writing ipconfig on Windows CMD and by writing ifconfig on Linux Terminal.

Upvotes: 4

Amit Sinha
Amit Sinha

Reputation: 614

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

Upvotes: 11

Sagar Acharya
Sagar Acharya

Reputation: 3767

just check if you have given the internet permission and the you have good internet connection

Upvotes: 1

Tisha
Tisha

Reputation: 1

Use this library here for fetching data, And don't declare response as final and http.Response, instead of this you can use var

Upvotes: 0

Related Questions