Connect Flutter to localhost server in real device

Viewed 2024

I've been searching about configuring Flutter to connect to localhost in a real device, but I haven't found anything.

I always get "Conecction timed out" when I make try to connect to the server in my localhost.

I've tried the application with the emulator with 10.0.0.2 and it worked, but now I want to try the connection in my device and it seems IMPOSSIBLE!

This is the error I get:

SocketException: OS Error: Connection timed out, errno = 110, address = 192.168.0.125, port = 41814

Please! It can't be such a difficult thing, I guess...

I'm using a Spring Boot server and Flutter. My device is OnePlus 6T.

When I use postman it responds fine, Idk what is happening here.

enter image description here

2 Answers

This solution only when you have WiFi-isolation enabled in Router. Otherwise, if you put your local IP address API hosting url & to flutter, it will work!

STEP 1

I had the same issue and I resolved it like this. By default (highly likely) your router comes with WiFi-isolation enabled. This is a security feature. First you need to turn off this & save settings. Power off the router for >= 1 minute and & turn it on.

Router WiFi-isolation setting

STEP 2

Then you need to add two new rules to your firewall. Allow rules as Port-TCP-inbound and Port-TCP-outbound rules with the hosting port (e.g. 8000). IP should be kept 'Any' & allow for public/private/domain. In your server API configure your application to run in your local IP with your above configured Port number (8000). It should be like this.

.NET Core Web API hosting URL

STEP 3

In your flutter application set your URL to your local PC IP & Port.

Flutter post request

Then run your API and flutter application. Your local IP-Port configuration will work for emulators, android devices & web. I don't have iOS device to test. It should work there as well.

If u mean Now you're using local server (ex. xampp) and want to test on real device Try this one https://ngrok.com/

Related