Connection refused error : Running flutter app in mobile, It is not connecting to server, but when it is running in web app it works well with server

1.1k views Asked by At

This is Flutter app with python(flask)

error-image

If I debug, it shows an exception

debugging image

If I run the flutter app in chrome, it is interacting with flask and gives the correct response. But when I run this app in mobile, it shows the above error. I have included the internet permission in Android.manifest file.

2

There are 2 answers

2
Mohamed Alsaid On

You can't reach localhost (127.0.0.1) from your phone, it works on chrome because the browser is running on the same machine as the server.

Use the local IP of your machine (e.g. 192.168.x.x) and make sure the phone/emulator is connected to the same network/wifi.

0
fayis dev On

If you are running your app on a real device and using localhost in Flask, that is most likely the problem. You cannot access your PC localhost directly on your mobile.

If you are connected to your phone via a USB cable, then you can use the Google Chrome port forwarding option.

Type chrome://inspect in the chrome search bar and select the port forwarding option.

Then set the port and IP address. In your case, do as follows

enter image description here

Press done, now you can use localhost in your phone.