Search code examples
androidnode.jsflutterport

Can't get Flutter app on real device to make http request on Node.j server running locally


I have my Node server running locally on 127.0.0.1:3000 and with my Flutter app running on Chrome localhost:5000 I can send http requests to url localhost:3000 and they all respond. I'm now trying running it on a real Android device connected via usb and with the same url I get error: SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 45614 error. Now, why port 45614 (from the error) gets used if the url localhost port is 3000?

I got my ip4 address and used that in my Flutter app url as was suggested in various posts here on SO, tried with and without port as static String dbUrl = '192.168.43.105'; or static String dbUrl = '192.168.43.105:3000'; but requests are not hitting the server. I tried setting adb to the Node.js server port 3000 with adb tcpip 3000and that restarted the ads server listening on port 3000 but still requests won't hit my Node.js server..

Running adb devices command gets me 5203e95d43cd93a7 device which is the connected Android tablet, and with adb forward --list I get 5203e95d43cd93a7 tcp:56393 tcp:41547 but I can't find out which port is which and how to use it.. Sorry for the probably messy question but as you can see I'm quite lost here and after reading various posts here on the subject I only got me more confused.. Any help would be very much appreciated. Many thanks.


Solution

  • Firstly you need to get your pc ip address, for example your ip address is 192.168.1.1 then add port which u are using in your backend like 8000 or 8080. so now your url will be like http://192.169.1.1:8000/. and make sure your server/backend is running and your device and laptop is connected with same network.

    It will work!